【问题标题】:check if file exists in php检查文件是否存在于php中
【发布时间】:2011-05-14 07:13:12
【问题描述】:
if (!(file_exists(http://mysite.com/images/thumbnail_1286954822.jpg))) {   
$filefound = '0';                         
}

为什么这不起作用?

【问题讨论】:

标签: php file-exists


【解决方案1】:
if (!file_exists('http://example.com/images/thumbnail_1286954822.jpg')) {   
$filefound = '0';
}

【讨论】:

  • if (!file_exists($base_url.'images/thumbnail_1286954822.jpg')) { $filefound = '0'; } 就像这样其实很抱歉。
  • @anonymous 其实不是这样 抱歉,你没有要求 $base_url + images/thumbnail_1286954822.jpg 你要求 http://mysite com/images/thumbnail_1286954822.jpg
【解决方案2】:
  1. 函数需要一个字符串。

  2. file_exists() 无法正常使用 HTTP URL。

【讨论】:

  • 你有什么解决办法吗?
【解决方案3】:

file_exists 检查指定路径中是​​否存在文件。

语法:

file_exists ( string $filename )

如果filename指定的文件或目录存在则返回TRUEFALSE 否则。

$filename = BASE_DIR."images/a/test.jpg";
if (file_exists($filename)){
    echo "File exist.";
}else{
    echo "File does not exist.";
}

您可以使用 getimagesize() 的另一种替代方法,如果指定路径中的文件/目录不可用,它将返回 0(零)。

if (@getimagesize($filename)) {...}

【讨论】:

    【解决方案4】:

    根据您对 Haim 的评论,这是您自己服务器上的文件吗?如果是这样,您需要使用文件系统路径,而不是 url(例如file_exists( '/path/to/images/thumbnail.jpg' ))。

    【讨论】:

      【解决方案5】:

      你也可以使用PHPget_headers()函数。

      例子:

      function check_file_exists_here($url){
         $result=get_headers($url);
         return stripos($result[0],"200 OK")?true:false; //check if $result[0] has 200 OK
      }
      
      if(check_file_exists_here("http://www.mywebsite.com/file.pdf"))
         echo "This file exists";
      else
         echo "This file does not exist";
      

      【讨论】:

      • 对我来说,file_exists() 不起作用。我正在尝试查找 PDF 文件。但你的解决方案奏效了。谢谢。你知道为什么 file_exists() 对我不起作用吗?
      • 这取决于代码。 stackoverflow.com/q/1287837/4366303 - 这可能会回答你
      【解决方案6】:

      对我来说,file_exists() 函数也无法正常工作。所以我得到了这个替代解决方案。希望这对某人有所帮助

      $path = 'http://localhost/admin/public/upload/video_thumbnail/thumbnail_1564385519_0.png';
      
          if (@GetImageSize($path)) {
              echo 'File exits';
          } else {
              echo "File doesn't exits";
          }
      

      【讨论】:

      • @Nguaial getimagesize() 函数将确定任何受支持的给定图像文件的大小,并返回尺寸以及文件类型和要在普通 HTML IMG 标记中使用的高度/宽度文本字符串以及对应的 HTTP 内容类型。
      【解决方案7】:

      检查下面的代码

      if ($user->image) {
              $filename = "images/" . $user->image;
              if (file_exists($filename)) {
                  echo '<br />';
                  echo "File exist.";
              } else {
                  echo '<br />';
                  echo "File does not exist.";
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2014-05-03
        • 1970-01-01
        • 1970-01-01
        • 2013-03-01
        • 2023-01-02
        • 2013-09-19
        • 2013-06-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多