简体   繁体   中英

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.

  • As you can see I do not try to require an entire directory even though the message says so.
  • I don't need class autoloader since I don't include classes even though I encounter the same error message in my autoloader when I loop through some paths when I search for my class file.
  • There's a possibility in the function (not shown here) that another path could be specified as a parameter so adding all those paths to the PHP include path would ruin the application considering I make multiple requires for classes, config files, models, controllers, views etc.
  • This is not exactly the code that I use in my application however illustrates what I'm trying to do.
  • The echoed path in the original code always outputs the correct path to the file I need so I'm sure the path is correct.
  • The path is extracted at run-time and then is cleaned also the slashes are corrected to match the current platform.

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM