Php 获取远程文件大小(不使用内容长度字段)

Php 获取远程文件大小(不使用内容长度字段),php,curl,Php,Curl,我需要得到远程文件的大小而不下载。我使用这段代码,但是一些站点的标题不包含内容长度:字段。在这种情况下,如何获得文件大小 $ch = curl_init("http://wordpress.org/latest.zip"); curl_setopt($ch, CURLOPT_NOBODY, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, true); curl_seto

我需要得到远程文件的大小而不下载。我使用这段代码,但是一些站点的标题不包含内容长度:字段。在这种情况下,如何获得文件大小

$ch = curl_init("http://wordpress.org/latest.zip");
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);
curl_close($ch);

$contentLength = 'unknown';
if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
  $contentLength = (int)$matches[1];
}
echo $contentLength;
这是echo$数据的结果

HTTP/1.1 200 OK
Server: nginx
Date: Tue, 10 Apr 2012 11:32:20 GMT
Content-Type: application/zip
Connection: close
Pragma: no-cache
Cache-control: private
Content-Description: File Transfer
Content-Disposition: attachment; filename=wordpress-3.3.1.zip
Content-MD5: d6332c76ec09fdc13db412ca0b024df9
X-nc: EXPIRED luv 139

若内容长度标头不存在,则表示文件大小未知。在您的情况下,您有内容处置头,这意味着可下载文件是服务器响应的附件(与电子邮件附件相同)。因此,如果没有Content-Length标头,则无法获取大小。

是否返回了标头,但名称是小写的“Content-Length”?或者冒号和实际值之间有空格。
echo$data
查看内容和headers@safarov,我将echo$数据的结果添加到问题中。@artaskerov-这可能有用: