SLC
SLC

Reputation: 2207

PHP - Require or Include doesn't work inside loops?

I'm trying to loop through a list of paths and require those PHP scripts using a foreach loop. However I get this error:

Warning: require_once(/opt/lampp/htdocs/framework): failed to open stream: No such file or directory in /opt/lampp/htdocs/framework/system/classes/config.cls.php on line 47

Fatal error: require_once(): Failed opening required '' (include_path='.:/opt/lampp/lib/php') in /opt/lampp/htdocs/framework/system/classes/config.cls.php on line 47

When I do the require function outside the loop using the same path it always works without any problems. (I'm using full paths not relative)

I've checked and tested this 100 times in 100 ways and whenever I require a file inside a loop I get an error.

I've tested on both Windows and Linux and with Windows I get "Access Denied...." or something like that.

I've tested on XAMPP 1.8.1 XAMPP 1.8.2, LAMPP 1.8.1 LAMPP 1.8.2, AMPPS 2.0 etc. again the same error every time.

I've checked the file path and permissions 100 times and I'm sure the path is not the cause.

Is this a PHP limitation or me doing somethin wrong ?

Code example:

$paths = array('path1', 'path2', path3);

foreach ($paths => $p) {
    if (file_exists($p) && require_once($p)) {
        break;
    }
}

The example code above would also be inside a method located in a static class

EDIT: I cannot add the original code because it uses multiple defines from multiple files and the error I encounter also happens everywhere I try to require a file inside a loop. However here's some code that illustrates much metter what I'm trying to do:

abstract class _config
{        
    public static function load($file_name)
    {
        $locations = array(
            '/opt/lampp/htdocs/framework/application/configs/',
            '/opt/lampp/htdocs/framework/system/configs/'
        );

        foreach ($location as $path) {
            $file = $path.$file_name.'.php';

            echo $file.'<br/>';

            if (file_exists($file) && require_once($file)) {
                break;
            }
        }
    }
}

When I use:

_config::load('test');

The output is:

/opt/lampp/htdocs/framework/application/configs/test.php
/opt/lampp/htdocs/framework/system/configs/test.php

And the error message....

When I try:

require_once('/opt/lampp/htdocs/framework/application/configs/test.php');
// or
require_once('/opt/lampp/htdocs/framework/system/configs/test.php');

Everything works fine.

Upvotes: 0

Views: 2196

Answers (2)

Moduo
Moduo

Reputation: 623

Try the scandir function of PHP to check whether a file is present or not. Then you've got the information you want and do not have to run a require/require_once/include in a loop.

Here's some information about the scandir function: http://php.net/manual/en/function.scandir.php

Hope this'll help!

Upvotes: 0

DevZer0
DevZer0

Reputation: 13535

what your trying to do is search through multiple paths for a specific file. The way you have implemented it is problematic. use the php include_path to provide the additional search paths for a specific file.

$paths = array('path1', 'path2', 'path3');
$paths[] = get_include_path();
set_include_path(implode(PATH_SEPERATOR, $paths));

and then you can do your require_once($file) which will search through all the paths defined in $path

Upvotes: 1

Related Questions