59
if (!(file_exists(http://mysite.com/images/thumbnail_1286954822.jpg))) {   
$filefound = '0';                         
}

为什么这行不通?

4

7 回答 7

92
if (!file_exists('http://example.com/images/thumbnail_1286954822.jpg')) {   
$filefound = '0';
}
于 2010-11-23T06:57:34.303 回答
36
  1. 该函数需要一个字符串。

  2. file_exists()不适用于 HTTP URL。

于 2010-11-23T06:57:49.303 回答
20

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

句法:

file_exists ( string $filename )

TRUE如果文件名指定的文件或目录存在,则返回;FALSE除此以外。

$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)) {...}
于 2017-01-26T04:51:04.947 回答
6

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

于 2010-11-23T07:44:56.213 回答
5

您还可以使用PHP get_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";
于 2019-10-17T07:56:15.207 回答
3

对我来说, 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";
    }
于 2019-07-31T05:06:15.203 回答
1

检查下面的代码

if ($user->image) {
        $filename = "images/" . $user->image;
        if (file_exists($filename)) {
            echo '<br />';
            echo "File exist.";
        } else {
            echo '<br />';
            echo "File does not exist.";
        }
    }
于 2022-01-05T11:45:04.190 回答