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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2252577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:44:09  来源:igfitidea点击:

Check if an include (or require) exists

phpincludeexistsrequire

提问by MintDeparture

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.

在调用之前如何检查 include / require_once 是否存在,我尝试将其放入错误块中,但 PHP 不喜欢那样。

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.

我认为file_exists()会付出一些努力,但是这需要整个文件路径,并且无法轻松地将相对包含传递给它。

Are there any other ways?

还有其他方法吗?

回答by Johannes Gorset

I believe file_existsdoes work with relative paths, though you could also try something along these lines...

我相信file_exists确实适用于相对路径,尽管您也可以尝试按照这些方式进行操作...

if(!@include("script.php")) throw new Exception("Failed to include 'script.php'");

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 includeis supressed by prefixing it with @.

...不用说,您可以用您选择的任何错误处理方法替换异常。这里的想法是 -if语句验证是否可以包含该文件,并且通常输出的任何错误消息include都通过添加前缀来抑制@

回答by Khawar

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*/ }

回答by Stephane JAIS

Check out the stream_resolve_include_path function, it searches with the same rules as include().

查看 stream_resolve_include_path 函数,它使用与 include() 相同的规则进行搜索。

http://php.net/manual/en/function.stream-resolve-include-path.php

http://php.net/manual/en/function.stream-resolve-include-path.php

回答by Yacoby

file_existswould 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.

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;
}

回答by Alix Axel

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

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

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

回答by jacmkno

I think the correct way is to do:

我认为正确的做法是:

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

This is because the documentationsays that stream_resolve_include_pathresolves the "filename against the include path according to the same rules as fopen()/include."

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

Some people suggested using is_fileor is_readablebut that′s not for the general use casebecause 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.

有些人建议使用is_fileoris_readable但这不适用于一般用例,因为在一般用例中,如果文件在 file_exists 返回 TRUE 后由于某种原因被阻止或不可用,那么您需要注意一个非常难看的错误消息就在最终用户的脸上,否则您很容易出现意外和莫名其妙的行为,可能会丢失数据和类似的事情。