gpt4 book ai didi

php - 获取远程文件大小(不使用 Content-Length 字段)

转载 作者:搜寻专家 更新时间:2023-10-31 22:13:15 24 4
gpt4 key购买 nike

我需要在不下载的情况下获取远程文件的大小。我使用此代码,但某些网站的 header 不包含 Content-Length: 字段。在这种情况下我如何获得文件大小?

$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 $data 的结果;

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 header 不存在,则意味着文件大小未知。在您的情况下,您有 Content-Disposition header ,这意味着可下载文件是服务器响应的附件(与电子邮件附件相同)。所以在这里,如果您没有 Content-Length header ,则无法获取大小。

关于php - 获取远程文件大小(不使用 Content-Length 字段),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10100471/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com