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


Checking if the images exist in the remote server issue

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

我已经尝试了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
}
?>

我的代码将最终显示图像,但这将需要很长时间。有没有减少时间或其他方法来做这件事?非常感谢!

检查远程服务器中是否存在图像的简单Java脚本代码

    <!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>

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

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

这不仅会使页面加载时间更好,它也会更准确,因为客户端可能有某些站点拦截器或防火墙,你的服务器没有