检查图像是否存在于远程服务器问题中

时间:2013-05-07 17:06:28

标签: php image curl

我正在尝试检查我的图像是否存在于远程服务器中。我有大量的图像(数百个)。

我已经尝试了curlfile_get_contents功能,这两个功能都会冻结我的浏览器,因为它需要很长时间才能检查。

我的结构是这样的

<?php
for loops{
 $ch = curl_init($imageFile[$i]);

       curl_setopt($ch, CURLOPT_NOBODY, true);
       curl_exec($ch);
       $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
       if(retcode==200){
          $imagePath = $imageFile[i]
       }else{
          $imagePth = 'badImage.gif';
       }
       curl_close($ch);

?>

//show images in table
<tr>  
  <td> <img src='".$imagePath."'/> </td>  //show the image if the images exist.
</tr>


<?php
}
?>

我的代码最终会显示图片,但需要很长时间。反正有没有减少时间或其他方式来做到这一点?非常感谢!

2 个答案:

答案 0 :(得分:1)

简单的javascript代码,用于检查远程服务器中是否存在图像

    <!DOCTYPE html>
<html>
<head>
<script src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
function Isimage(url) {
$("<img>", {
    src: url,
    error: function() { alert('image not found'); return false; },
    load: function() { alert('image found'); return true; }
});
}
</script>
</head>
<body>
<input type="text" id="url" name="img_url" onblur="Isimage(this.value);"/>
</body>
</html>

答案 1 :(得分:0)

某些服务器无法正确响应您的CURL_NOBODY选项正在使用的HEAD请求。他们将连接打开的时间过长。您可以尝试设置超时,但大多数PHP安装仅具有卷曲超时的第二个粒度。

鉴于这些令人讨厌的规则,我可能会放一些尝试加载文件的JavaScript。如果文件不可用,请显示badImage.gif,否则显示该文件。

这不仅会使页面加载时间更好,而且更准确,因为客户端可能有某些站点阻止程序或服务器没有的防火墙。