检查图像是否存在php
问题描述:
我正在编写一个属性门户网站。我被困在检查图像。我知道如何检查是否设置了图片网址。但问题是检测在url上是否有实际的有效图像。检查图像是否存在php
例如:http://property.images.themovechannel.com/cache/7217/6094437/img_main.jpg
,但图像已经消除,因此只是显示在我的欢迎使用属性搜索页面空白此图片的URL存在。有没有一种方法可以检查URL中是否有图像,然后在不存在的情况下显示占位符。
像
$imageURL = "http://property.images.themovechannel.com/cache/7217/6094437/img_main.jpg";
if (exists($imageURL)) { display image }
else { display placeholder }
但所有这确实是检查URL存在,它确实有事先只是没有像有
感谢
答
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';
}
答
可以use file_exists
功能检查,如果图像存在,但你应该给图像
if (file_exists(dirname($_SERVER['SCRIPT_FILENAME']).'/'.$filename))
{
// your code here
}
也许你可以看看在返回的HTML标签的物理路径? – silkfire 2013-02-20 00:41:40
我在想,但页面是相当大的一种想要做的一切,在飞行中使用PHP – 2013-02-20 00:44:15
你可以发布一个链接到_does_存在的图像? – silkfire 2013-02-20 00:46:49