检查包含(或要求)是否存在

发布于 2024-08-20 20:48:07 字数 150 浏览 7 评论 0原文

在调用之前如何检查 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

后来的我们 2024-08-27 20:48:07

我相信 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 by include is supressed by prefixing it with @.

温柔女人霸气范 2024-08-27 20:48:07

查看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

葬花如无物 2024-08-27 20:48:07

您还可以检查包含文件中定义的任何变量、函数或类,并查看包含是否有效。

if (isset($variable)) { /*code*/ }

if (function_exists('function_name')) { /*code*/ }

if (class_exists('class_name')) { /*code*/ }

You can also check for any variables, functions or classes defined in the include file and see if the include worked.

if (isset($variable)) { /*code*/ }

OR

if (function_exists('function_name')) { /*code*/ }

OR

if (class_exists('class_name')) { /*code*/ }
心的位置 2024-08-27 20:48:07

file_exists() 适用于相对路径,它还会检查目录是否存在。使用 is_file() 代替:

if (is_file('./path/to/your/file.php'))
{
    require_once('./path/to/your/file.php');
}

file_exists() works with relative paths, it'll also check if directories exist. Use is_file() instead:

if (is_file('./path/to/your/file.php'))
{
    require_once('./path/to/your/file.php');
}
萌面超妹 2024-08-27 20:48:07

file_exists 将用于检查所需文件相对于当前工作目录是否存在,因为它与相对路径一起工作得很好。但是,如果包含文件位于 PATH 上的其他位置,则必须检查多个路径。

function include_exists ($fileName){
    if (realpath($fileName) == $fileName) {
        return is_file($fileName);
    }
    if ( is_file($fileName) ){
        return true;
    }

    $paths = explode(PS, get_include_path());
    foreach ($paths as $path) {
        $rp = substr($path, -1) == DS ? $path.$fileName : $path.DS.$fileName;
        if ( is_file($rp) ) {
            return true;
        }
    }
    return false;
}

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.

function include_exists ($fileName){
    if (realpath($fileName) == $fileName) {
        return is_file($fileName);
    }
    if ( is_file($fileName) ){
        return true;
    }

    $paths = explode(PS, get_include_path());
    foreach ($paths as $path) {
        $rp = substr($path, -1) == DS ? $path.$fileName : $path.DS.$fileName;
        if ( is_file($rp) ) {
            return true;
        }
    }
    return false;
}
一念一轮回 2024-08-27 20:48:07

我认为正确的方法是:

if(file_exists(stream_resolve_include_path($filepath))){
  include $filepath;    
}

这是因为 文档 表示 stream_resolve_include_path 解析“根据与 fopen()/include 相同的规则针对包含路径的文件名”。

有些人建议使用 is_fileis_read这不适合一般用例,因为在一般使用中,如果文件被阻止或在 file_exists 返回 TRUE 后由于某种原因不可用,这是你需要注意的事情,最终用户的脸上会出现非常难看的错误消息,否则你以后可能会遇到意外和无法解释的行为,可能会丢失数据之类的事情那。

I think the correct way is to do:

if(file_exists(stream_resolve_include_path($filepath))){
  include $filepath;    
}

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 or is_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文