We've all seen this error before:

Warning: require_once(test/blah.inc) [function.require-once]: failed to open stream: No such file or directory in /home/narf/narf.inc on line 3

Fatal error: require_once() [function.require]: Failed opening required 'test/blah.inc' (include_path='.:/usr/lib64/php:/usr/lib/php:/home/narf/inc:/home3/narf/php:/home3/narf/inc/dev:') in /home/narf/narf.inc on line 3

Super annoying.

Here's what I'm looking for: Some sort of PHP snippet that will list the paths that are being searched for the include file.

From what I can see, I've set up my path correctly (the above include scenario is simply a fabrication), and I can include the file successfully from other portions of the project.

However, I just can't get it to work. If I had a list of locations that were being searched, I could easily track down the path that I'm missing and identify why my require/include isn't working.

Thoughts? Does someone have a snippet kicking around they could share?

Thanks a million in advance! :)

Edit: For what it's worth, I think I tracked down the problem, but if you've still got something that could help out, it would be great for future misconfigurations! :)

Edit 2: Thanks for all who have answered. Getting the include path is nice, but I'm looking for actual absolute paths of directories that are being searched. The reason why I'm being picky is because it gets confusing when using a require() or include() along with relative paths. Also, I suspect that there are some other gotchas such as (potentially) whether or not symlinks are followed on a given setup, etc.

Comments

Ugh. Finally found the right path. It turns out I needed: " require_once('../../../www/blog/wp-load.php');" This makes NO sense, but at least my script is running now. Maybe I'll whip up a script for everyone later that lists all searched absolute paths.

Written by rinogo

Also, I should have mentioned originally that I'm using relative paths (sometimes with '../x/y/z', and other times just with 'x/y/z') in my include statement (as shown above), so PHP's include path is only partially relevant (only in the case of the 'x/y/z' inclusion). From include()'s documentation: "If a path is defined — whether absolute ... or relative to the current directory (starting with . or ..) — the include_path will be ignored altogether."

Written by rinogo

Accepted Answer

Quoting the manual's page of include :

Files are included based on the file path given or, if none is given, the include_path specified.
If the file isn't found in the include_path, include() will finally check in the calling script's own directory and the current working directory before failing


And the include_path is a configuration directive that (quoting) :

Specifies a list of directories where the require(), include(), fopen(), file(), readfile() and file_get_contents() functions look for files.

It can be configured in your php.ini file, or at runtime, using the set_include_path() function.

And, of course, to get the include path, you have get_include_path().

Written by Pascal MARTIN
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki