Joh*_*set 56
我相信file_exists确实可以使用相对路径,但你也可以尝试这些方面的东西......
if(!@include("script.php")) throw new Exception("Failed to include 'script.php'");
...不用说,您可以将异常替换为您选择的任何错误处理方法.这里的想法是if-statement验证文件是否可以被包含,并且通常输出的任何错误消息include都通过为其加上前缀来抑制@.
小智 9
您还可以检查包含文件中定义的任何变量,函数或类,并查看包是否有效.
if (isset($variable)) { /*code*/ }
Run Code Online (Sandbox Code Playgroud)
要么
if (function_exists('function_name')) { /*code*/ }
Run Code Online (Sandbox Code Playgroud)
要么
if (class_exists('class_name')) { /*code*/ }
Run Code Online (Sandbox Code Playgroud)
查看stream_resolve_include_path函数,它使用与include()相同的规则进行搜索.
http://php.net/manual/en/function.stream-resolve-include-path.php
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;
}
Run Code Online (Sandbox Code Playgroud)
file_exists()使用相对路径,它还会检查目录是否存在.is_file()改为使用:
if (is_file('./path/to/your/file.php'))
{
require_once('./path/to/your/file.php');
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
74016 次 |
| 最近记录: |