Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP file include script not fully working on LINUX machines

I have a lot of functions and classes that I have included in my website. With the help of stackoverflow I recieved a script that automaticly includes all files in a folder and its subfolders: PHP: Automatic 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 it gives problems with extension of classes.

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. This all fully works on windows but refuses to work on linux.

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

The code I use is the following:

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 anyone tell me how it is that windows has no problems with extension classes and linux does? Also is there a fix for the problem without having to manual include the base classes?

like image 538
matthy Avatar asked Nov 30 '22 00:11

matthy


2 Answers

Have you verified that AcumulusExportBase is included before AcumulusExportWorkshop under Linux? PHP is sensitive to the order of imports.

like image 118
Mark Leighton Fisher Avatar answered Dec 01 '22 14:12

Mark Leighton Fisher


Both other answers are correct (and I've upvoted them both). Your problem will be the order the files are loaded (see Mark's response) and the recursion is also wrong (see KIKO).

However there is a better way of doing what you want: use an autoloader. http://php.net/manual/en/language.oop5.autoload.php

First time is confusing, but once you've grasped it, it's a lovely way of loading files.

Basically you say "If I need class X and it's not loaded, then load file Y.php".

If you're being super-lazy and don't want to specify each class then you can say "If I need class X and it's not loaded, run through the directory structure looking for a file called X.php and load that, my class will be in there." You can mix in what you have above to do this.

This way, you can load AcumulusExportWorkshop first, and then it looks for AcumulusExportBase afterwards and runs happily.

And, more beneficially, you only load what you need. If you never need the class, it never gets loaded.

like image 33
Robbie Avatar answered Dec 01 '22 12:12

Robbie