php file_exists() 返回 false,但文件确实存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6930150/
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
file_exists() returns false, but the file DOES exist
提问by Bv202
I'm having a very weird issue with file_exists(). I'm using this function to check if 2 different files in the same folders do exist. I've double-checked, they BOTH do exist.
我对 file_exists() 有一个非常奇怪的问题。我正在使用此功能来检查同一文件夹中是否存在 2 个不同的文件。我仔细检查过,它们都存在。
echo $relative . $url['path'] . '/' . $path['filename'] . '.jpg';
Result: ../../images/example/001-001.jpg
echo $relative . $url['path'] . '/' . $path['filename'] . '.' . $path['extension'];
Result: ../../images/example/001-001.PNG
Now let's use file_exists() on these:
现在让我们对这些使用 file_exists() :
var_dump(file_exists($relative . $url['path'] . '/' . $path['filename'] . '.jpg'));
Result: bool(false)
var_dump(file_exists($relative . $url['path'] . '/' . $path['filename'] . '.' . $path['extension']));
Result: bool(true)
I don't get it - both of these files do exist. I'm running Windows, so it's not related to a case-sensitive issue. Safe Mode is off.
我不明白 - 这两个文件都存在。我正在运行 Windows,因此它与区分大小写的问题无关。安全模式已关闭。
What might be worth mentioning though is that the .png one is uploaded by a user via FTP, while the .jpg one is created using a script. But as far as I know, that shouldn't make a difference.
值得一提的是,.png 文件是由用户通过 FTP 上传的,而 .jpg 文件是使用脚本创建的。但据我所知,这应该没什么区别。
Any tips?
有小费吗?
Thanks
谢谢
采纳答案by Timur
Results of the file_exists()
are cached, so try using clearstatcache()
. If that not helped, recheck names - they might be similar, but not same.
的结果file_exists()
被缓存,所以尝试使用clearstatcache()
. 如果这没有帮助,请重新检查名称 - 它们可能相似,但不相同。
回答by Mahdi Loghmani
file_exists()
just doesn't work with HTTP addresses.
file_exists()
只是不适用于 HTTP 地址。
It only supports filesystem paths (and FTP, if you're using PHP5.)
它仅支持文件系统路径(和 FTP,如果您使用的是 PHP5。)
Please note:
请注意:
Works :
作品:
if (file_exists($_SERVER['DOCUMENT_ROOT']."/folder/test.txt")
echo "file exists";
Does not work:
不起作用:
if (file_exists("www.mysite.com/folder/test.txt")
echo "file exists";
回答by ace
It's because of safe mode. You can turn it off or include the directory in safe_mode_include_dir
. Or change file ownership / permissions for those files.
这是因为安全模式。您可以将其关闭或将目录包含在safe_mode_include_dir
. 或者更改这些文件的文件所有权/权限。
回答by Shahar
I found that what works for me to check if a file exists (relative to the current php file it is being executed from) is this piece of code:
我发现检查文件是否存在(相对于它正在执行的当前 php 文件)对我有用的是这段代码:
$filename = 'myfile.jpg';
$file_path_and_name = dirname(__FILE__) . DIRECTORY_SEPARATOR . "{$filename}";
if ( file_exists($file_path_and_name) ){
// file exists. Do some magic...
} else {
// file does not exists...
}
回答by Brian
Just my $.02: I just had this problem and it was due to a space at the end of the file name. It's not always a path problem - although that is the first thing I check - always. I could cut and paste the file name into a shell window using the ls -l command and of course that locates the file because the command line will ignore the space where as file_exists does not. Very frustrating indeed and nearly impossible to locate were it not for StackOverflow.
只是我的 $.02:我刚刚遇到了这个问题,这是由于文件名末尾有一个空格。这并不总是一个路径问题——尽管这是我检查的第一件事——总是。我可以使用 ls -l 命令将文件名剪切并粘贴到 shell 窗口中,当然可以定位文件,因为命令行将忽略 file_exists 不存在的空间。如果不是 StackOverflow,确实非常令人沮丧,几乎不可能找到它。
HINT: When outputting debug statements enclose values with delimiters () or [] and that will show a space pretty clearly. And always remember to trim your input.
提示:当输出调试语句时,用分隔符 () 或 [] 将值括起来,这将非常清楚地显示一个空格。并且永远记得修剪你的输入。
回答by Doug
Try using DIRECTORY_SEPARATOR
instead of '/' as separator. Windows uses a different separator for file system paths (backslash) than Linux and Unix systems.
尝试使用DIRECTORY_SEPARATOR
而不是 '/' 作为分隔符。Windows 使用与 Linux 和 Unix 系统不同的文件系统路径分隔符(反斜杠)。
回答by Julien Ricard
It can also be a permission problem on one of the parent folders or the file itself.
它也可能是父文件夹之一或文件本身的权限问题。
Try to open a session as the user running your webserver and cd into it. The folder must be accessible by this user and the file must be readable.
尝试以运行您的网络服务器的用户身份打开一个会话,然后 cd 进入该会话。该用户必须可以访问该文件夹,并且该文件必须是可读的。
If not, php will return that the file doesn't exist.
如果没有,php 将返回该文件不存在。
回答by Mangilal Sharma
A very simple trick is here that worked for me.
一个非常简单的技巧在这里对我有用。
When I write following line, than it returns false.
当我写下一行时,它返回false。
if(file_exists('/my-dreams-files/'.$_GET['article'].'.html'))
And when I write with removing URL starting slash, then it returns true.
当我写删除 URL 开始斜杠时,它返回 true。
if(file_exists('my-dreams-files/'.$_GET['article'].'.html'))
回答by scipilot
I have a new reason this happens - I am using PHP inside a Docker container with a mounted volume for the codebase which resides on my local host machine.
我有一个发生这种情况的新原因 - 我在 Docker 容器中使用 PHP,该容器为我的本地主机上的代码库安装了一个卷。
I was getting file_exists == FALSE
(inside Composer autoload
), but if I copied the filepath
into terminal - it did exist! I tried the clearstatche()
, checked safe-mode was OFF.
我正在获取file_exists == FALSE
(在 Composer 内部autoload
),但是如果我将其复制filepath
到终端中 - 它确实存在!我试过clearstatche()
,检查安全模式是关闭的。
Then I remembered the Docker volume mapping: the absolute path on my local host machine certainly doesn't exist inside the Docker container - which is PHP's perspective on the world.
然后我想起了 Docker 卷映射:我本地主机上的绝对路径肯定不存在于 Docker 容器内——这是 PHP 对世界的看法。
(I keep forgetting I'm using Docker, because I've made shell functions which wrap the docker run commands so nicely...)
(我一直忘记我在使用 Docker,因为我已经制作了 shell 函数,可以很好地包装 docker run 命令......)
回答by Irshad Khan
I spent the last two hours wondering what was wrong with my if statement: file_exists($file)
was returning false, however I could call include($file)
with no problem.
我花了最后两个小时想知道我的 if 语句有什么问题:file_exists($file)
返回 false,但是我可以include($file)
毫无问题地调用。
It turns out that I didn't realize that the php include_path
value I had set in the .htaccess
file didn't carry over to file_exists
, is_file
, etc.
事实证明,我没有意识到的是,PHP的include_path
,我有在设定值.htaccess
文件不结转到file_exists
,is_file
等等。
Thus:
因此:
<?PHP
// .htaccess php_value include_path '/home/user/public_html/';
// includes lies in /home/user/public_html/includes/
//doesn't work, file_exists returns false
if ( file_exists('includes/config.php') )
{
include('includes/config.php');
}
//does work, file_exists returns true
if ( file_exists('/home/user/public_html/includes/config.php') )
{
include('includes/config.php');
}
?>
Just goes to show that "shortcuts for simplicity" like setting the include_path in .htaccess can just cause more grief in the long run.
只是表明“简单的快捷方式”,例如在 .htaccess 中设置 include_path 从长远来看只会引起更多的悲伤。