【问题标题】:Check if image file exist检查图像文件是否存在
【发布时间】:2013-01-13 23:20:21
【问题描述】:

我想检查一个 img 文件是否存在,否则我使用默认的 img。

但我想用来确保图像文件存在的检查不起作用。

我有以下代码。

$filename = "http://".$_SERVER['SERVER_NAME']."/media/img/".$row['CatNaam'].".jpg";
        echo"  <img src=\"".$filename."\" alt=\"".$row['CatNaam']."\">";

        echo "filename";

        if (file_exists($filename)) {
            echo "The file $filename exists";
        } else {
            echo "The file $filename does not exist";
        }

图像在那里我可以看到它,但它说图像不存在。 如果我从 echo filename 复制响应,则该文件就在那里。

编辑:

我将代码更改为

$filename = "/media/img/".$row['CatNaam'].".jpg";
        echo $filename;
        echo"  <img src=\"".$filename."\" alt=\"".$row['CatNaam']."\">";


         echo "<br> $filename <br>";
        if (file_exists($filename)) {
            echo "The file $filename exists";
        } else {
            echo "The file $filename does not exist";
        }

我仍然可以看到图像,但现在我收到了不同的警告(我想这比以前更好)

警告说:

arning: file_exists() [function.file-exists]: open_basedir 限制生效。文件(/media/img/Badkraan.jpg)不在允许的路径内:(审查)在第 71 行的 mydomain/public_html/ve/paginas/producten/zoek.php 文件 /media/img/Badkraan.jpg 不存在

文件权限为755

【问题讨论】:

  • 图片/包含文件夹有什么权限?
  • 看我的回答。使用 dirname 获取基本目录。让我们举个例子:假设你得到类似:'/home/youruser/domain/public_html/scripts'。然后你的目录就是那个 dir 。 '../media/img'.

标签: php


【解决方案1】:

来源:http://us1.php.net/file_exists

<?php
    $filename = '/path/to/foo.txt';

    if (file_exists($filename)) {
        echo "The file $filename exists";
    } else {
        echo "The file $filename does not exist";
    }
?>

来源:http://us1.php.net/manual/en/function.is-file.php

<?php
    var_dump(is_file('a_file.txt')) . "\n";
    var_dump(is_file('/usr/bin/')) . "\n";
?>

【讨论】:

    【解决方案2】:
    <?php
    $imgFile = "http://www.technew.in/uploads/image/samsung_galaxy_xpro_2_technew_in.jpg";
    if (getimagesize($imgFile)) {
      echo "Valid image file";
    } else {
      echo "Invalid image file or file not exist";
    }
    ?>
    

    【讨论】:

      【解决方案3】:

      检查这个:

      $file = 'Your_file';
      $file_headers = @get_headers($file);
      
      if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
         // does not exist
      } else {
         // exist     
      }
      

      【讨论】:

        【解决方案4】:

        尝试以下方法

        • 确保$filename 得到正确验证
        • 只有在你的 PHP 中激活 allow_url_fopen 时它才会起作用 配置
        • 确保您已授予图像文件夹的权限

        【讨论】:

          【解决方案5】:

          您正在检查客户端看到的文件(http...)。相反,您需要检查服务器磁盘上的实际文件,即,如果您使用的是 linux,则类似于:

          if(file_exists($PATH_TO_FILES . '/' . $row['CatNaam'] . ".jpg")) ...
          

          其中 $PATH_TO_FILES 可以从以下内容开始计算:dirname(__FILE__)),这将为您提供当前脚本所在的目录。

          【讨论】:

            【解决方案6】:

            对于由于安全模式限制而无法访问的文件,此函数返回 FALSE。 http://php.net/manual/en/function.file-exists.php

            【讨论】:

              【解决方案7】:

              这可能是file_exists 对通过 http 协议提供的文件无法正常工作。至少在我的情况下是这样的(即使allow_url_fopen 设置为On)。我假设文件来自您无法直接访问文件系统的不同服务器。否则,只使用文件本身的完整路径会更容易。

              不过你也可以这样试试。这不是很好,但它至少应该完成工作。您应该可以在 php.net 页面上找到file_exists 的其他示例。

              $filename = "http://".$_SERVER['SERVER_NAME']."/media/img/".$row['CatNaam'].".jpg";
              echo"  <img src=\"".$filename."\" alt=\"".$row['CatNaam']."\">";
              echo "filename";
              
              $file_headers = @get_headers($filename);
              if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
                  echo "The file $filename does not exist";
              }
              else {
                  echo "The file $filename exists";
              }
              

              【讨论】:

              • 文件与我的网站位于同一台服务器上,我是具有 755(tryd 777 无差异)权限的媒体目录
              • 那么使用绝对文件路径而不是http-url可能会更容易。
              【解决方案8】:

              我有同样的问题,我通过给出实际地址来解决它,它很漂亮,但它对我有用:

              1-首先获取当前位置的地址并将其放入数组中:

              $pieces = explode("/", __FILE__);
              

              2-更改地址以适合您想要的文件:

              array_pop($pieces);
              

              3-终于把它放在一起了:

              $final = implode("/", $pieces);
              

              4-然后将其放入文件存在函数中。

              【讨论】:

                【解决方案9】:

                $filename = "http://im.rediff.com/money/2011/jan/19sld3.jpg";

                    $file_headers = @get_headers($filename);
                
                    if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
                    //return false; 
                    echo "file not found";
                    }else {
                    //return true;  
                    echo "file found";
                
                    }
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2016-01-27
                  • 1970-01-01
                  • 2011-01-12
                  • 1970-01-01
                  • 2011-03-20
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多