Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP函数GETIMAGESIZE在60秒内超时以检查远程文件_Php_Image_Url_Timeout - Fatal编程技术网

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

PHP函数GETIMAGESIZE在60秒内超时以检查远程文件,php,image,url,timeout,Php,Image,Url,Timeout,我使用getimagesize检查图像是否存在 图像位于远程URL中,因此我检查了一个链接 如果图像存在,则响应将在2秒内给出 如果图像不存在,也没有图像错误链接,则在不到2秒内给出响应 问题是当图像不存在,并且有一个链接说(未找到图像)或类似的东西时。。。。getimagesize在整整60秒内一直试图定位图像(我使用php microtime进行了检查) 其他方法也会发生同样的事情,需要60秒的响应时间。。。我试过使用curl、文件内容、标题、imagecreatefromjpeg。。。。所

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

图像位于远程URL中,因此我检查了一个链接

如果图像存在,则响应将在2秒内给出

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

问题是当图像不存在,并且有一个链接说(未找到图像)或类似的东西时。。。。getimagesize在整整60秒内一直试图定位图像(我使用php microtime进行了检查)

其他方法也会发生同样的事情,需要60秒的响应时间。。。我试过使用curl、文件内容、标题、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

希望能有所帮助。:)