【发布时间】:2015-02-01 04:33:40
【问题描述】:
我正在尝试验证目录中是否存在文件。当我使用此代码时,它可以工作 - 显示图像:
<?php
$imgId=0;
$filename='../uploadedimages/project-'.$item->id.'-'.$imgId;
echo "<img src='".$filename."' ></img>";
?>
当我对 file_exists 函数使用相同的代码时,它不起作用:
<?php
$imgId=0;
$filename='../uploadedimages/project-'.$item->id.'-'.$imgId;
if (file_exists($filename)) {
echo "<img src='".$filename."' ></img>";
}
?>
我的问题很简单:WTF??
【问题讨论】:
-
您的文档根目录与服务器的文档根目录不一样...所以
file_exists()使用脚本所在的文件夹作为参考目录。加载<img>需要从您的网络服务器的根目录中引用它。 -
file_exists需要文件系统路径。 -
旁注:
</img>你不需要那个。 -
相对路径对于服务器来说也很麻烦
-
阅读"WTF"手册php.net/manual/en/function.file-exists.php ;)
$filename='/var/user/you/httpdocs/uploadedimages/project...
标签: php file find file-exists