使用curl限制文件大小

时间:2015-06-17 16:04:08

标签: php curl

我在下载之前使用此代码来控制文件大小:

$info = array();
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_HEADER, true); 
curl_setopt($ch, CURLOPT_NOBODY, true); // make it a HEAD request
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch,CURLOPT_USERAGENT,$this->userAgent());
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,$webinfo['ctimeout']); 
curl_setopt($ch, CURLOPT_TIMEOUT, $webinfo['timeout']); //timeout in seconds 
$head = curl_exec($ch);

$info['type'] = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$info['size'] = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
$info['path'] = parse_url($url, PHP_URL_PATH);
$info['filename'] = substr($info['path'], strrpos($info['path'], '/') + 1);
$info['http'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$info['url'] = $url;
$info['active'] = false;

if(curl_errno($ch))
  {
    echo curl_error($ch);
    curl_close($ch);
    return false;
  }
if($info['http'] == 200)
  {
    $info['active'] = true;
    curl_close($ch);

    if( $info['size'] > 15728640)
      {
        return false;
      }
     return $info;
}

所以我限制它大约15mb,但我今天检查了我的服务器,看到它下载的文件超过200mb。

这段代码我错了什么?

我想也许'15728640'错了,或者我应该用其他格式写它?

0 个答案:

没有答案
相关问题