PHP Curl 获取下载文件大小

标签 php cookies curl filesize

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

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 结果的两个不同 header :

工作一个:

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

有什么想法吗?

最佳答案

在工作中,您似乎在运行 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/

关于PHP Curl 获取下载文件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10992079/

相关文章:

java - 如何在imageview上设置捕获的图像

php - 如果有人嗅探了您加密的 cookie 数据并将其原封不动地发送到服务器怎么办?

java - 如果浏览器不允许 cookie 怎么办

php - 使用 PHP 和 cURL 防止远程服务器欺骗

php - Mysql查询通过匹配id连接两个表和组

php - 如何通过使用php获取mysql数据以父子方式构建数组

php - 在脚本末尾在网站上绘制图像

c# - 检查浏览器是否接受 cookie

curl - 与 file_get_contents() 相比,CURL 是否昂贵?

php - 在php中的数组中发布数据