Php 检查图像是否存在 我正在编写一个房地产门户网站。我一直在检查图像。我知道如何检查是否设置了图像url。但问题在于检测url上是否存在有效的图像

Php 检查图像是否存在 我正在编写一个房地产门户网站。我一直在检查图像。我知道如何检查是否设置了图像url。但问题在于检测url上是否存在有效的图像,php,Php,例如: 此图像url存在,但该图像现在已被删除,因此它仅在“我的属性搜索”页面中显示为空白。是否有一种方法可以检查url上是否有图像,如果不存在则显示占位符 差不多 $imageURL = "http://property.images.themovechannel.com/cache/7217/6094437/img_main.jpg"; if (exists($imageURL)) { display image } else { display placeholder } 但所有这些

例如:

此图像url存在,但该图像现在已被删除,因此它仅在“我的属性搜索”页面中显示为空白。是否有一种方法可以检查url上是否有图像,如果不存在则显示占位符

差不多

$imageURL = "http://property.images.themovechannel.com/cache/7217/6094437/img_main.jpg";

if (exists($imageURL)) { display image } 
else { display placeholder }
但所有这些只是检查url是否存在,它确实存在,只是那里没有图像

提前感谢

用于确保URL指向有效的图像

if (getimagesize($imageURL) !== false) {
    // display image
}

也许你可以在返回的HTML中寻找一个标记?我正在考虑这个问题,但是这个页面非常大,你想用phpC动态地完成这一切吗?你可以发布一个链接到一个确实存在的图像吗?对于外部URI来说,这是一个非常慢的函数。如果文件不存在,它会引起php的注意。使用带有“@”的函数是不好的方式,所以我认为更好的方式是@plutov.by
function exists($uri)
{
    $ch = curl_init($uri);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    return $code == 200;
}
function is_webUrl($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    // don't download content
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if (curl_exec($ch) !== FALSE) {
        return true;
    } else {
        return false;
    }
}

if(is_webUrl('http://www.themes.tatwerat.com/wp/ah-personal/wp-content/uploads/2016/08/features-ah-wp-view.jpg')) {
   echo 'yes i found it';
}else{
   echo 'file not found';
}