if (!(file_exists(http://mysite.com/images/thumbnail_1286954822.jpg))) {
$filefound = '0';
}
为什么这行不通?
if (!(file_exists(http://mysite.com/images/thumbnail_1286954822.jpg))) {
$filefound = '0';
}
为什么这行不通?
if (!file_exists('http://example.com/images/thumbnail_1286954822.jpg')) {
$filefound = '0';
}
该函数需要一个字符串。
file_exists()
不适用于 HTTP URL。
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)) {...}
根据您对 Haim 的评论,这是您自己服务器上的文件吗?如果是这样,您需要使用文件系统路径,而不是 url(例如file_exists( '/path/to/images/thumbnail.jpg' )
)。
您还可以使用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";
对我来说, 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";
}
检查下面的代码
if ($user->image) {
$filename = "images/" . $user->image;
if (file_exists($filename)) {
echo '<br />';
echo "File exist.";
} else {
echo '<br />';
echo "File does not exist.";
}
}