PHP函数GETIMAGESIZE超时60秒检查远程文件


PHP function GETIMAGESIZE timeout in 60 seconds to check remote files

我使用getimagesize来检查图像是否存在

图片是在一个远程URL,所以我检查一个链接。

如果图像存在,则在不到2秒内给出响应。

如果图像不存在,也没有图像错误链接,则在2秒内给出响应。

问题是当图像不存在,有一个链接说(图像未找到)或类似的....getimagesize一直试图定位图像正好60秒(我检查了PHP microtime)。

其他方法也发生同样的事情,需要60秒的响应…我已经尝试过curl,与file_get_contents, get_headers, imagecreatefromjpeg....它们都需要60秒才能返回false。

有什么办法可以减少这个时间吗?

尝试将此函数用于CURLOPT_TIMEOUT:

function checkRemoteFile($url)
{
  $timeout = 5; //timeout seconds
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  // don't download content
  curl_setopt($ch, CURLOPT_NOBODY, 1);
  curl_setopt($ch, CURLOPT_FAILONERROR, 1);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt ($ch, CURLOPT_TIMEOUT, $timeout);
  return (curl_exec($ch)!==FALSE);
}

$image = "/img/image.jpg";
if ( checkRemoteFile($image) )
{
  $info = getimagesize($image);
  print_r($info); //Print image info
  list($width, $height, $type, $attr) = $info; //Store image info
}
else
  echo "Timeout";

您也可以使用稍有不同的CURLOPT_CONNECTTIMEOUT

希望能有所帮助。:)