php 即使文件存在,file_exists() 也会返回 false(远程 URL)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10444059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
file_exists() returns false even if file exist (remote URL)
提问by Szymon Toda
My file_exists()returns false even if provided image to check https://www.google.pl/logos/2012/haring-12-hp.pngexist. Why?
file_exists()即使提供的要检查的图像https://www.google.pl/logos/2012/haring-12-hp.png存在,我也会返回 false 。为什么?
Below I am presenting full failing PHP code ready to fire on localhost:
下面我将展示准备在本地主机上触发的完整失败的 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";
}
回答by Gilly
$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";
}
回答by Maurizio Brioschi
A better if statement that not looks at http version
一个更好的 if 语句,不看 http 版本
$file_headers = @get_headers($remote_filename);
if (stripos($file_headers[0],"404 Not Found") >0 || (stripos($file_headers[0], "302 Found") > 0 && stripos($file_headers[7],"404 Not Found") > 0)) {
//throw my exception or do something
}
回答by Amber
As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support stat() family of functionality.
自 PHP 5.0.0 起,此函数还可与某些 URL 包装器一起使用。请参阅支持的协议和包装器以确定哪些包装器支持 stat() 系列功能。
From the http(s) pageon Supported Protocols and Wrappers:
Supports stat() No
Supports stat() No
回答by vinod inti
$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";
}
回答by Сергей Савельев
function check_file ($file){
if ( !preg_match('/\/\//', $file) ) {
if ( file_exists($file) ){
return true;
}
}
else {
$ch = curl_init($file);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($code == 200){
$status = true;
}else{
$status = false;
}
curl_close($ch);
return $status;
}
return false;
}
回答by Ayman Safadi
What you need is something like url_exists. Refer to the comments in the file_existsdocs: http://php.net/manual/en/function.file-exists.php
你需要的是类似的东西url_exists。请参阅file_exists文档中的注释:http: //php.net/manual/en/function.file-exists.php
Here's one of the examples posted:
这是发布的示例之一:
<?php
function url_exists($url){
$url = str_replace("http://", "", $url);
if (strstr($url, "/")) {
$url = explode("/", $url, 2);
$url[1] = "/".$url[1];
} else {
$url = array($url, "/");
}
$fh = fsockopen($url[0], 80);
if ($fh) {
fputs($fh,"GET ".$url[1]." HTTP/1.1\nHost:".$url[0]."\n\n");
if (fread($fh, 22) == "HTTP/1.1 404 Not Found") { return FALSE; }
else { return TRUE; }
} else { return FALSE;}
}
?>

