如何检查网址是否指向图片文件,以及 http://website.com/images-stories/gallery/image-name.jpg
如果该位置没有文件,则默认为另一个URL(对于默认文件)。
我正在使用file_exists,但意识到检查路径不是网址。
<?PHP
while ($data = mysqli_fetch_assoc($result)):
$smaller_img = $data['smaller_img'];
$url = $smaller_img;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true); // we want headers
curl_setopt($ch, CURLOPT_NOBODY, true); // we don't need body
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
$output = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpcode=200)
{
$smaller_imgf=$url;
} else {
$smaller_imgf='../images/submit_icon.png';
}
?>
<img src="<?php echo $smaller_imgf; ?>
答案 0 :(得分:2)