本文介绍了即使文件存在(远程URL),file_exists()也会返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使提供的用于检查https://www.google.pl/logos/2012/haring-12-hp.png的图像存在,我的file_exists()也会返回false.为什么?

My file_exists() returns false even if provided image to check https://www.google.pl/logos/2012/haring-12-hp.png exist. Why?

下面,我将介绍完整的失败PHP代码,准备在localhost上启动:

Below I am presenting full failing PHP code ready to fire on localhost:

$filename = 'https://www.google.pl/logos/2012/haring-12-hp.png';
echo "<img src=" . $filename . " />";
if (file_exists($filename)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}

推荐答案

$filename= 'https://www.google.pl/logos/2012/haring-12-hp.png';
$file_headers = @get_headers($filename);

if($file_headers[0] == 'HTTP/1.0 404 Not Found'){
      echo "The file $filename does not exist";
} else if ($file_headers[0] == 'HTTP/1.0 302 Found' && $file_headers[7] == 'HTTP/1.0 404 Not Found'){
    echo "The file $filename does not exist, and I got redirected to a custom 404 page..";
} else {
    echo "The file $filename exists";
}

这篇关于即使文件存在(远程URL),file_exists()也会返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 14:08