file_exists相对路径不起作用


file_exists with relative path not working

file_exists不起作用。也尝试过realpath..同样的问题

首先检查文件是否存在。 file_exists返回 false,但文件仍然加载

chdir(__DIR__.'/../..');
$file = 'frontend.php';
echo "$file'n";
if(file_exists($file)){
    echo "File found'n";
}
else{
    echo "File not found'n";
}
require $file;

输出

frontend.php
File not found
Contents of frontend.php

正如 php.net/file_exists 所说,file_exists() 函数需要:

文件或目录的路径。

因此,请尝试在前面加上目录的路径:

if (file_exists(dirname(__FILE__) . $file)) {
    echo "File found'n";
}

> 有时file_exists()会缓存其结果。您可以尝试clearstatcache()清除缓存。

尝试这样做:

$file = dirname(__FILE__) . '/frontend.php';
if(file_exists($file)){
    //...
}