检查包含(或要求)是否存在
在调用之前如何检查 include / require_once 是否存在,我尝试将其放入错误块中,但 PHP 不喜欢这样。
我认为 file_exists() 会需要一些努力,但这需要整个文件路径,并且无法轻松地将相对包含传递到其中。
还有其他方法吗?
How do you check if an include / require_once exists before you call it, I tried putting it in an error block, but PHP didn't like that.
I think file_exists()
would work with some effort, however that would require the whole file path, and a relative include could not be passed into it easily.
Are there any other ways?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我相信
file_exists
确实适用于相对路径,尽管您也可以尝试按照这些方式进行操作...if(!@include("script.php")) throw new Exception("Failed包含 'script.php'");
...不用说,您可以用该异常替换您选择的任何错误处理方法。这里的想法是
if
语句验证是否可以包含该文件,并且通常由include
输出的任何错误消息都会通过在其前面添加@< 来抑制。 /代码>。
I believe
file_exists
does work with relative paths, though you could also try something along these lines...if(!@include("script.php")) throw new Exception("Failed to include 'script.php'");
... needless to say, you may substitute the exception for any error handling method of your choosing. The idea here is that the
if
-statement verifies whether the file could be included, and any error messages normally outputted byinclude
is supressed by prefixing it with@
.查看stream_resolve_include_path函数,
它使用与 include() 相同的规则进行搜索。
http://php.net/manual/en/function.stream-resolve-include-路径.php
Check out the stream_resolve_include_path function,
it searches with the same rules as include().
http://php.net/manual/en/function.stream-resolve-include-path.php
您还可以检查包含文件中定义的任何变量、函数或类,并查看包含是否有效。
或
或
You can also check for any variables, functions or classes defined in the include file and see if the include worked.
OR
OR
file_exists()
适用于相对路径,它还会检查目录是否存在。使用is_file()
代替:file_exists()
works with relative paths, it'll also check if directories exist. Useis_file()
instead:file_exists
将用于检查所需文件相对于当前工作目录是否存在,因为它与相对路径一起工作得很好。但是,如果包含文件位于 PATH 上的其他位置,则必须检查多个路径。file_exists
would work with checking if the required file exists when it is relative to the current working directory as it works fine with relative paths. However, if the include file was elsewhere on PATH, you would have to check several paths.我认为正确的方法是:
这是因为 文档 表示
stream_resolve_include_path
解析“根据与 fopen()/include 相同的规则针对包含路径的文件名”。有些人建议使用
is_file
或is_read
但这不适合一般用例,因为在一般使用中,如果文件被阻止或在 file_exists 返回 TRUE 后由于某种原因不可用,这是你需要注意的事情,最终用户的脸上会出现非常难看的错误消息,否则你以后可能会遇到意外和无法解释的行为,可能会丢失数据之类的事情那。I think the correct way is to do:
This is because the documentation says that
stream_resolve_include_path
resolves the "filename against the include path according to the same rules as fopen()/include."Some people suggested using
is_file
oris_readable
but that´s not for the general use case because in the general use, if the file is blocked or unavailable for some reason after file_exists returns TRUE, that´s something you need to notice with a very ugly error message right on the final user´s face or otherwise you are open to unexpected and inexplicable behavior later with possible loss of data and things like that.