PHP Curl获取下载文件大小

5

我在两台不同的服务器上使用这个脚本:

function curlGetFileInfo($url, $cookies="default"){
global $_config;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'serverpath/cookies/'.$cookies.'.txt');
$data = curl_exec($ch);
curl_close($ch); 
if ($data === false) { 
    return 0;
}
//echo $data;   
$info['filename'] = get_between($data, 'filename="', '"');
$info['extension'] = end(explode(".",$info['filename']));
if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
  $info['filesize'] = (int)$matches[1];
}
return $info;
}

这些服务器具有相同的PHP版本和相同的PHP-Curl版本。curl结果有两个不同的头部:
有效的:
HTTP/1.1 302 Found Date: Tue, 12 Jun 2012 07:04:35 GMT Server: Apache/2.2.16 (Debian) X-Powered-By: PHP/5.3.3-7+squeeze13 Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache,must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Location: http://stor1076.uploaded.to/dl/b3411ded-0f45-4efc-b705-8c8ac89b5e41 Vary: Accept-Encoding Connection: close Content-Type: text/html HTTP/1.1 200 OK Server: nginx/1.0.5 Date: Tue, 12 Jun 2012 07:04:35 GMT Content-Type: video/x-msvideo Content-Length: 733919232 Last-Modified: Tue, 29 May 2012 15:10:07 GMT Connection: keep-alive Content-Disposition: attachment; filename="Saw.[Spanish.DVDRip].[XviD-Mp3].by.SDG.avi" Accept-Ranges: bytes
无效的:
HTTP/1.1 302 Found Date: Tue, 12 Jun 2012 07:05:26 GMT Server: Apache/2.2.16 (Debian) X-Powered-By: PHP/5.3.3-7+squeeze13 Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Location: http://stor1164.uploaded.to/dl/22c3d242-365d-4e1e-b903-f1e2b81812c2 Vary: Accept-Encoding Connection: close Content-Type: text/html
Cookie被正确设置(包含登录信息),而且其他简单的Curl功能正常工作。
另外,我执行了curl_getinfo($ch, CURLINFO_HTTP_CODE)并得到了以下结果:
有效的:200
无效的:302
有什么想法?

好的,这是一个open_basedir问题。谢谢大家。 - Biwu
2个回答

1

在工作中,您似乎同时运行了Apache和nginx。您可以看到有两个HTTP响应:

HTTP/1.1 302 Found Date: Tue, 12 Jun 2012 07:04:35 GMT Server: Apache/2.2.16 (Debian) HTTP/1.1 200 OK Server: nginx/1.0.5

因此,您的设置不同。我不知道它们如何精确地一起运行,但这提供了一些见解,可能会帮助您解决问题:http://kbeezie.com/view/apache-with-nginx/


它们不是“一起运行的”,第一个请求只是被重定向了。 - alexn
好的,这是一个 open_basedir 问题。无论如何,谢谢伙计! - Biwu
我的意思是在服务器配置方面“一起运行”。但无论如何,问题都不同。 - kasimir

1

好的,这是一个open_basedir问题。谢谢大家。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接