2013-05-07 197 views
0

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

我试过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 
} 
?> 

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

+1

存在图像中有你的PHP尝试file_exists功能? – dee 2013-05-07 17:10:31

+0

您可以通过使用AJAX轮询脚本以获取图像来进行渐进式加载。我假设现在的问题是,该页面正在永久加载,因为你的PHP脚本是从字面上下载每一个图像文件。 – thatidiotguy 2013-05-07 17:10:41

+0

@webgal是的。加载时间相同。对于thatidiotguy你是什么意思,通过使用Ajax拉脚本? – Rouge 2013-05-07 17:16:33

回答

0

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

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

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

1

简单的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> 
相关问题