检查图像文件是否存在

时间:2013-01-30 07:49:01

标签: php

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

但我想使用的检查确保图像文件有dosnt工作。

我有以下代码。

$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 = "/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不存在

filepermission是755

9 个答案:

答案 0 :(得分:2)

来源: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";
?>

答案 1 :(得分:1)

检查一下:

$file = 'Your_file';
$file_headers = @get_headers($file);

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

答案 2 :(得分:0)

尝试以下事项

  • 确保$filename已正确验证
  • 只有在PHP中激活allow_url_fopen时才能使用它 配置
  • 确保您已获得图片文件夹的许可

答案 3 :(得分:0)

您正在检查文件,因为客户端看到它(http ...)。您需要检查服务器磁盘上的实际文件,即如果您使用的是Linux:

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

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

答案 4 :(得分:0)

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

答案 5 :(得分:0)

对于通过http协议提供的文件,file_exists可能无法正常工作。至少就像我的情况一样(即使将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";
}

答案 6 :(得分:0)

<?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";
}
?>

答案 7 :(得分:0)

我有同样的问题,我通过提供实际地址来解决它,它非常漂亮,但它对我有用:

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

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

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

array_pop($pieces);

3 - 最后把它放在一起:

$final = implode("/", $pieces);
然后把它放在文件存在函数中。

答案 8 :(得分:0)

$ 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";

    }