开发者

How to list require_once()'s searched paths in PHP?

开发者 https://www.devze.com 2023-02-17 18:15 出处:网络
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

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 u开发者_如何学Csing 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.


You want the function get_include_path() Documentation


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().


The path is listed in the error message: it's the values in the include_path

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号