我在两个服务器上工作(A和B,我需要在A上显示来自B的一些图像。问题是我需要测试图像是否存在(否则我只显示客户端的名称)。
当我在本地测试时,这是有效的:
if (file_exists(http://www.example.com/images/3.jpg)) {
// I show the file here
} else {
// I just show the name
}
但是,这现在不起作用。救命!
您不能将file_exists
用于检查远程文件。
使用get_headers
函数并检查文件是否返回200
。
$file_headers = get_headers('http://www.example.com/images/3.jpg');
if ($file_headers[0] == 'HTTP/1.1 200 OK') {
echo "The file exists";
} else {
echo "The file doesn't exist";
}
尝试使用此
$array= get_headers("http://images.visitcanberra.com.au/images/canberra_hero_ima.jpg");
echo $h= $array[0];
如果不存在,它将返回HTTP/1.1 404 Not Found;如果存在,则返回HTTP/1.1 200 OK
function is_file_on_url_exists($url)
{
if (@file_get_contents($url, 0, NULL, 0, 1))
{
return true;
}
return false;
}
试试这个!!