php 如何测试php中是否存在远程图像文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14806159/
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
How do I test if an remote image file exists in php?
提问by TijuanaKez
This spits out a whole heap of NO's but the images are there and path correct as they are displayed by <img>.
这会吐出一大堆 NO,但图像在那里并且路径正确,因为它们由<img>.
foreach ($imageNames as $imageName)
{
$image = 'http://path/' . $imageName . '.jpg';
if (file_exists($image)) {
echo 'YES';
} else {
echo 'NO';
}
echo '<img src="' . $image . '">';
}
回答by Chris Christensen
file_exists looks for a local path, not an "http://" URL
file_exists 查找本地路径,而不是“http://” URL
use:
用:
$file = 'http://www.domain.com/somefile.jpg';
$file_headers = @get_headers($file);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
$exists = false;
}
else {
$exists = true;
}
回答by Ionic? Biz?u
file_existsuses local path, not a URL.
file_exists使用本地路径,而不是 URL。
A solution would be this:
一个解决方案是这样的:
$url=getimagesize(your_url);
if(!is_array($url))
{
// The image doesn't exist
}
else
{
// The image exists
}
See this for more information.
有关详细信息,请参阅此内容。
Also, looking for the response headers (using the get_headersfunction) would be a better alternative. Just check if the response is 404:
此外,寻找响应头(使用该get_headers函数)将是一个更好的选择。只需检查响应是否为 404:
if(@get_headers($your_url)[0] == 'HTTP/1.1 404 Not Found')
{
// The image doesn't exist
}
else
{
// The image exists
}
回答by realization
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($retcode==200) echo 'YES';
else echo 'NO';
回答by Christie
This is what I did. It covers more possible outcomes with getting the headers because if you can't access the file it's not always just "404 Not Found". Sometimes it's "Moved Permanently", "Forbidden", and other possible messages. However it's just " 200 OK" if the file exists, and can be accessed. The part with HTTP can have 1.1, or 1.0 after it, which is why I just used strpos to be more reliable for every situation.
这就是我所做的。它涵盖了获取标题的更多可能结果,因为如果您无法访问该文件,它并不总是只是“404 Not Found”。有时它是“永久移动”、“禁止”和其他可能的消息。但是,如果文件存在并且可以访问,则它只是“ 200 OK”。带有 HTTP 的部分可以有 1.1 或 1.0,这就是为什么我只使用 strpos 以便在每种情况下都更可靠。
$file_headers = @get_headers( 'http://example.com/image.jpg' );
$is_the_file_accessable = true;
if( strpos( $file_headers[0], ' 200 OK' ) !== false ){
$is_the_file_accessable = false;
}
if( $is_the_file_accessable ){
// THE IMAGE CAN BE ACCESSED.
}
else
{
// THE IMAGE CANNOT BE ACCESSED.
}
回答by Fujael
function remote_file_exists($file){
$url=getimagesize($file);
if(is_array($url))
{
return true;
}
else {
return false;
}
$file='http://www.site.com/pic.jpg';
echo remote_file_exists($file); // return true if found and if not found it will return false

