doudu7626 2014-05-21 15:53
浏览 7
已采纳

PHP如何检查图片网址是否为false [复制]

This question already has an answer here:

How can I check if a url directs to an image file, and http://website.com/images-stories/gallery/image-name.jpg

and if there is no file in the location, default to another url (for a default file).

I was using file_exists but realize that checks a path not a url.

<?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; ?>
</div>
  • 写回答

1条回答 默认 最新

  • douou6696 2014-05-21 15:56
    关注

    You can look with curl for status code 200

    https://stackoverflow.com/a/11797723/2441442

    If 200, you can check for image with getimagesize

    http://www.php.net/manual/de/function.getimagesize.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题