内容长度上限为 2 GB

发布于 2024-11-30 18:52:51 字数 1622 浏览 0 评论 0原文

我创建了一个让用户下载文件的脚本:

function file_size($filename)
{
    exec('stat -c %s ' . escapeshellarg($filename), $return);
    return (float)$return[0];
}

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($filename));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . file_size($filename));

readfile($filename);

exit;

非常简单。 file_size 函数可让我检测大于 2 GB 的文件大小。

问题Content-length 永远不会超过 2 GB:

< HTTP/1.1 200 OK
< Date: Sun, 21 Aug 2011 09:33:20 GMT
< Server: Apache
< Content-Description: File Transfer
< Content-Disposition: attachment; filename=very-large-file
< Content-Transfer-Encoding: binary
< Expires: 0
< Cache-Control: must-revalidate, post-check=0, pre-check=0
< Pragma: public
< Content-Length: 2147483647
< Content-Type: application/octet-stream

'Content-Length: ' 上执行 var_dump 。 file_size($filename) 返回string(26)“内容长度:4689218232”。如果我在没有 PHP 脚本的情况下直接访问该文件,则没有问题,并且 Apache 会报告正确的文件大小:

< HTTP/1.1 200 OK
< Date: Sun, 21 Aug 2011 09:58:33 GMT
< Server: Apache
< Last-Modified: Thu, 06 Jan 2011 21:56:47 GMT
< ETag: "8ba8f5e0-1177fcab8-49934940b30e5"
< Accept-Ranges: bytes
< Content-Length: 4689218232

但我真的很想通过我的 PHP 脚本提供该文件。感谢您抽出时间。

I've created a script that lets the user download files:

function file_size($filename)
{
    exec('stat -c %s ' . escapeshellarg($filename), $return);
    return (float)$return[0];
}

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($filename));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . file_size($filename));

readfile($filename);

exit;

Very simple. The file_size function lets me detect file sizes larger than 2 GB.

The problem is that Content-length never is above 2 GB:

< HTTP/1.1 200 OK
< Date: Sun, 21 Aug 2011 09:33:20 GMT
< Server: Apache
< Content-Description: File Transfer
< Content-Disposition: attachment; filename=very-large-file
< Content-Transfer-Encoding: binary
< Expires: 0
< Cache-Control: must-revalidate, post-check=0, pre-check=0
< Pragma: public
< Content-Length: 2147483647
< Content-Type: application/octet-stream

Doing a var_dump on 'Content-Length: ' . file_size($filename) returns string(26) "Content-Length: 4689218232". If I access the file directly without a PHP script, there is no problem and Apache is reporting the correct file size:

< HTTP/1.1 200 OK
< Date: Sun, 21 Aug 2011 09:58:33 GMT
< Server: Apache
< Last-Modified: Thu, 06 Jan 2011 21:56:47 GMT
< ETag: "8ba8f5e0-1177fcab8-49934940b30e5"
< Accept-Ranges: bytes
< Content-Length: 4689218232

But I'd really like to serve the file through my PHP script. Thank you for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千笙结 2024-12-07 18:52:51

使用 readfile 发送大文件并不是一个好习惯。

使用X-Sendfile,如下所示:header("X-Sendfile: $filename");。您需要 apache mod_sendfile。这也应该可以解决您的文件大小问题。

Sending huge files with readfile is not good practice.

Use X-Sendfile, like this: header("X-Sendfile: $filename");. You'll need apache mod_sendfile. This should also solve your file-size problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文