【问题标题】:PHP - file_exists doesn't work even if file exists in directoryPHP - 即使文件存在于目录中,file_exists 也不起作用
【发布时间】: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() 使用脚本所在的文件夹作为参考目录。加载&lt;img&gt; 需要从您的网络服务器的根目录中引用它。
  • file_exists 需要文件系统路径。
  • 旁注:&lt;/img&gt; 你不需要那个。
  • 相对路径对于服务器来说也很麻烦
  • 阅读"WTF"手册php.net/manual/en/function.file-exists.php ;) $filename='/var/user/you/httpdocs/uploadedimages/project...

标签: php file find file-exists


【解决方案1】:

您可以使用:$_SERVER['DOCUMENT_ROOT'] 了解您的位置。

然后尝试这样的事情:

$filename=$_SERVER['DOCUMENT_ROOT'].'uploadedimages/project-'.$item->id.'-'.$imgId;

但首先你需要确保路径存在。

【讨论】:

    【解决方案2】:

    在linux服务器上,在使用file_exists函数之前尝试将文件访问权限更改为755

    chmod 755 文件名.ext

    【讨论】:

      猜你喜欢
      • 2014-12-18
      • 2017-10-03
      • 2012-03-01
      • 2013-08-29
      • 2023-03-17
      • 2012-05-13
      • 2017-05-14
      • 2017-03-11
      • 1970-01-01
      相关资源
      最近更新 更多