PHP file includes script not fully working on LINUX machines

I have many functions and classes that I have included in my site. Using stackoverflow, I got a script that automatically includes all the files in a folder and its subfolders: PHP: Automatically include

When testing the script, it always worked, and I never had any problems with it. But recently, when switching from a Windows server to a linux server, there are problems with class extension.

PHP Fatal error: class "AcumulusExportBase" not found in path / functions / classes / Acumulus / AcumulusExportWorkshop.php on line 3, referer: pagesite /? page_id = 346

AcumulusExportWorkshop extends from AcumulusExportBase. All this fully works on Windows, but refuses to work on Linux.

I can fix this by creating include_once 'AcumulusExportBase.php'; but if there is a better solution, it all seems unnecessary and nullifying the work.

The code I use is as follows:

load_folder(dirname(__FILE__));

function load_folder($dir, $ext = '.php') {
    if (substr($dir, -1) != '/') { $dir = "$dir/"; }
    if($dh = opendir($dir)) {
        $files = array();
        $inner_files = array();
        while($file = readdir($dh)) {
            if($file != "." and $file != ".." and $file[0] != '.') {
                if(is_dir($dir . $file)) {
                    $inner_files = load_folder($dir . $file);
                    if(is_array($inner_files)) $files = array_merge($files, $inner_files); 
                } else {
                    array_push($files, $dir . $file);
                }
            }
        }
        closedir($dh);
        foreach ($files as $file) {
            if (is_file($file) and file_exists($file)) {
                $lenght = strlen($ext);
                if (substr($file, -$lenght) == $ext && $file != 'loader.php') { require_once($file); }
            }
        } 
    }
}

Can someone tell me how this is due to the fact that windows have no problems with extension classes and linux? Also, is there a fix for the problem without manually including base classes?

+4
source share
10 answers

Have you confirmed that AcumulusExportBase is enabled before AcumulusExportWorkshop on Linux? PHP is sensitive to import order.

+6
source

( ). , (. ), (. KIKO).

, : . http://php.net/manual/en/language.oop5.autoload.php

, , .

: " X, , Y.php".

- , : " X, , , X.php ". , , .

, AcumulusExportWorkshop, AcumulusExportBase .

, , , . , .

+5

, , , Windows. . , , - . "inner_files", , , , . . , "require_once", , "" . : , .

load_folder(dirname(__FILE__));

function load_folder($dir,$ext = '.php')
{
  if (substr($dir,-1) != '/') $dir = $dir.'/';
  if ($handle = opendir($dir))
  {
    while($file = readdir($handle))
    {
      if (($file != '.') && ($file != '..') && ($file[0] != '.'))
      {
        if (is_dir($dir.$file)) load_folder($dir.$file,$ext);
        else
        {
          if ((substr($file,-strlen($ext)) == $ext) &&
              ($file != 'loader.php') && 
              file_exists($dir.$file)) require_once($dir.$file);
        }
      }
    }
    closedir($handle);
  }
}

linux . , $ext load_folder().

- , . , . , , . , .

+2

, , .

() , , .

:

  • .

  • - . ( Windows Linux )

  • , .

  • .

, :

  • , - . , .., , , .

  • - , .

  • , , , , , , , .

. , .

PHP - , , , , .

, . . - .

, , , PHPUnit - (http://jes.st/2011/phpunit-bootstrap-and-autoloading-classes/)

class ClassDirectoryAutoLoader {
    static private $classNamesDirectory = array();

    public static function crawlDirectory($directory) {
        $dir = new DirectoryIterator($directory);
        foreach ($dir as $file) {
            self::addClassesAndCrawlDirectories($file);
        }
    }

        private static function addClassesAndCrawlDirectories($file){
            if (self::isRealDirectory($file)) {
                self::crawlDirectory($file->getPathname());
            } elseif (self::isAPhpFile($file)) {
                self::saveClassFilename($file);
            }
        }

            private static function isRealDirectory($file){
                // ignore links, self and parent
                return $file->isDir() && !$file->isLink() && !$file->isDot();
            }

            private static function isAPhpFile($file){
                //ends in .php
                return substr($file->getFilename(), -4) === '.php';
            }

            private static function saveClassFilename($file){
                //assumes that the filename is the same as the classname
                $className = substr($file->getFilename(), 0, -4);
                self::registerClass($className, $file->getPathname());
            }

    public static function registerClass($className, $fileName) {
        self::$classNamesDirectory[$className] = $fileName;
    }

    public static function loadClass($className) {
        if (isset(self::$classNamesDirectory[$className])) {
            require_once(self::$classNamesDirectory[$className]);
        }
    }
}

$classDir = dirname(__FILE__) . '/../classes'; // replace with the root directory for your class files

ClassDirectoryAutoLoader::crawlDirectory($classDir);

spl_autoload_register(array('ClassDirectoryAutoLoader', 'loadClass'));

,

  • ( Dir), .php.

  • , .

  • ( loadClass).

, , , :

  • , .

  • , .

, :

  • .

  • - , (. ).

  • , , .

  • , .

  • loadClass , .

:

  • .

  • .

  • .

  • (.. , ).

.

, :

  • .

  • .

  • .

  • .

  • .

+2

, , . , :

function endswith($string,$tidbit){
// Length of string
$strlen = strlen($string);

// Length of ending
$tidlen = strlen($tidbit);

// If the tidbit is of the right length (less than or equal to the length of the    string)
if($tidlen <= $strlen){

    // Substring requires a place to start the copying
    $tidstart = $strlen - $tidlen;

    // Get $tidlen characters off the end of $string
    $endofstring = substr($string, $tidstart, $tidlen);

    // If the $tidbit matches the end of the string
    $ret = ($endofstring == $tidbit);
    return $ret;
} else {
    // Failure
    return -1;
}
}

// Working
function ush_load_path($path) {

if (is_dir($path)) {
    if (is_file($path . '/' . (explode('/', $path)[count(explode('/', $path)) - 1]) . '.inc')) {
        require_once $path . '/' . (explode('/', $path)[count(explode('/', $path)) - 1]) . '.inc';
    }
    ush_load_path_recursive($path);

    // If it is a file   
} else if (is_file($path)) {
    require_once $path;

    // Failure
} else {
    return false;
}
}

function ush_load_path_recursive($path) {
// Directory RESOURCE
$path_dir = opendir($path);

// Go through the entries of the specified directory
while (false != ($entry = readdir($path_dir))) {
    if ($entry != '.' && $entry != '..') {
        // Create Full Path
        $path_ext = $path . '/' . $entry;

        // Development
        if (is_dir($path_ext)) {
            ush_load_path_recursive($path_ext);
        } else if (is_file($path_ext)) {
            if (ush_is_phplib($path_ext)) {
                print $path_ext . '<br />';
                require_once $path_ext;
            } else {
                // Do nothing
            }
        }
    }
}
}

// Working
function ush_is_phplib($path) {
   return endswith($path, '.inc');
}
+2
load_folder(dirname(__FILE__));

function load_folder($dir, $ext = '.php') {
if (substr($dir, -1) != '/') { $dir = "$dir/"; }

clearstatcache(); // added to clear path cache

if($dh = opendir($dir)) {
    $files = array();
    $inner_files = array();
    while($file = readdir($dh)) {
        if($file != "." and $file != ".." and $file[0] != '.') {
            if(is_dir($dir . $file)) {
                $inner_files = load_folder($dir . $file);
                if(is_array($inner_files)) $files = array_merge($files, $inner_files); 
            } else {
                array_push($files, $dir . $file);
            }
        }
    }
    closedir($dh);


    clearstatcache($dir); // added to clear path cache


    foreach ($files as $file) {
        if (is_file($file) and file_exists($file)) {
            $lenght = strlen($ext);
            if (substr($file, -$lenght) == $ext && $file != 'loader.php') { require_once($file); }
        }
    } 
}

}

, clearstatcache ($ path) symlink'd. Php symlink'd dirs .

+1

print_r ($ files) ($ dh); foreach, , ?

+1

, , , script... , , php, , , script .

- , ? , .

, , .

, script. , , :

spl_autoload_register('Modulehandler::Autoloader'); 

, , PHP .

:

static function Autoloader($className) { 
    $files = array($className); 
    $lowerClass = strtolower($className); 
    if (strcmp($className, $lowerClass) != 0) $files[] = $lowerClass; 

    foreach (self::$modules as $moduleName => $module) { 
        foreach ($files as $className) { 
            $file = "{$module}/classes/{$className}.php"; 
            if (file_exists($file)) { 
                require $file; 
                break; 
            } 
        } 
    } 
}

, , , , . , .

, , , .

+1

, AcumulusExportBase AcumulusExportWorkshop? , Linux , .

LIKE a.JPG a.jpg , LINUX .

+1

- . Mac Windows, , Linux "Capitalized.php" "CAPITALIZED.PHP"

.

+1

All Articles