php - 即使文件存在(远程 URL),file_exists() 也会返回 false

标签 php

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

下面我展示了准备在本地主机上启动的完整失败的 PHP 代码:

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

关于php - 即使文件存在(远程 URL),file_exists() 也会返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10444059/

相关文章:

php - 语法错误,意外标记 "<<",期望 "function"或 "const"

php - 有什么方法可以用 PHP/MySQL 动态创建流程图或 UML?

php - 拉拉维尔。如何根据 Content-Type 定义路由

javascript - Ajax方法submit()不起作用

php - 仅更新一条记录的表

php - 在 PHP 中比较文件校验和

php - 使用PHP将字符串分成相等的部分

php - PHP异常,错误并提供用户反馈

php - 如何使用 perl 读取序列化的 php 数据

php - 选择同一行并显示多次