0

我想检查一个 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

4

9 回答 9

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";
?>
于 2013-11-28T15:03:13.287 回答
1

检查这个:

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

if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
   // does not exist
} else {
   // exist     
}
于 2013-07-23T09:16:53.980 回答
1
<?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";
}
?>
于 2013-01-30T08:10:17.417 回答
0

尝试以下操作

  • 确保$filename经过适当验证
  • 它仅allow_url_fopen在您的 PHP 配置中激活时才有效
  • 确保您已授予图像文件夹的权限
于 2013-01-30T07:52:46.783 回答
0

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

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

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

于 2013-01-30T07:52:57.347 回答
0

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

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

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

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

array_pop($pieces);

3-最后把它放在一起:

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

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

于 2013-05-09T16:01:24.490 回答
0

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

于 2013-01-30T07:56:36.650 回答
0

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";
}
于 2013-01-30T07:58:11.447 回答
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";

    }
于 2015-04-01T10:33:28.997 回答