PHP file_exists 不起作用
我在 PHP 中的 file_exists
函数上遇到了问题,它总是返回 false,尽管文件在那里,因为我可以删除 if 语句,并且它显示得很好。
$filename = $_SERVER['DOCUMENT_ROOT']."/images/profilepictures/1.png";
if (file_exists($filename) == true)
{
$output .= '<img src="'.$filename.'" alt="profile picture" width="200"/>';
}
$filename
回显为:
/home/content/k/e/r/kernelkev/html/images/profilepictures/1.png
我一直在谷歌上搜索这个,大多数答案都是使用 DOCUMENT_ROOT,但它仍然对我不起作用。
谁能解释一下这个问题,因为它现在真的很烦我。
这似乎解决了它......
$filename = "/images/profilepictures/1.png";
if (file_exists("..".$filename))
{
$output .= '<img src="'.$filename.'" alt="profile picture" width="150"/>';
}
我不知道为什么,但我们开始了。
I have been having problems with file_exists
function in PHP that it always returns false, though the file is there as I can remove the if statement and it shows up fine.
$filename = $_SERVER['DOCUMENT_ROOT']."/images/profilepictures/1.png";
if (file_exists($filename) == true)
{
$output .= '<img src="'.$filename.'" alt="profile picture" width="200"/>';
}
The $filename
echos as:
/home/content/k/e/r/kernelkev/html/images/profilepictures/1.png
I have been googling this and most answers are to use the DOCUMENT_ROOT, but it still does not work for me.
Could anyone shed some light on this as it is really annoying me now.
This seemed to fix it...
$filename = "/images/profilepictures/1.png";
if (file_exists("..".$filename))
{
$output .= '<img src="'.$filename.'" alt="profile picture" width="150"/>';
}
I have no idea why, but there we go.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您在 php.ini 中使用 open_basedir 并对 open_basedir 路径之外的文件使用 file_exists,则即使文件确实存在,您也不会在日志中收到警告,并且 file_exists 返回 false。
如果文件权限未为“其他”启用且不属于您的 php 用户,则 file_exists 将无法找到您的文件。我以为我遇到了目录名称中包含空格的问题 (/users/andrew/Pictures/iPhoto Library/AlbumData.xml),但实际情况是图片、iPhoto Library 或 AlbumData.xml 没有读取权限。一旦我解决了这个问题, file_exists 就起作用了。
当我们正确配置服务器时,这些对我们来说可以正常工作,没有问题
If you use open_basedir in php.ini and use file_exists for file outside open_basedir path, you will not be warned at log and file_exists returns false even if file really exists.
file_exists will have trouble finding your file if the file permissions are not read enabled for 'other' when not owned by your php user. I thought I was having trouble with a directory name having a space in it (/users/andrew/Pictures/iPhoto Library/AlbumData.xml) but the reality was that there weren't read permissions on Pictures, iPhoto Library or AlbumData.xml. Once I fixed that, file_exists worked.
These will work fine for us with no problem as we confiugured server properly
如果文件夹被禁止,则 file_exists 不起作用。尝试将文件夹权限更改为 777 或 755 吗?
file_exists not working if the folder is forbidden. Try change your folder priviledge to 777 or 755 ?