使用 PHP 下载时,文件大小是否有限制?
尝试使用通常的 PHP 强制下载文件:
header("Content-type: $type" );
header("Content-Disposition: attachment; filename=$name");
header('Content-Length: ' . filesize($path));
对于小于 32 mb 的文件,它可以成功下载。对于更大的文件,它只返回归零的文件。
显然存在某种限制,但是什么限制了它呢?使用 Apache 2.2.11 和 PHP 5.3.0。
Trying to force-download file with PHP using usual:
header("Content-type: $type" );
header("Content-Disposition: attachment; filename=$name");
header('Content-Length: ' . filesize($path));
And it does successfully for files somewhere below 32 mb. For bigger ones it just returns zeroed file.
Obviously there's some kind of limit, but what sets it? Using Apache 2.2.11 and PHP 5.3.0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我最终偶然发现了这篇文章: http://w-shadow.com/blog/2007/08/12/how-to-force-file-download-with-php/。
添加所有推荐的标题并
在任何输出之前使用: - 有所帮助。没有更多可观察到的限制。文件下载完全,即使是大文件。
I eventually stumbled on this post: http://w-shadow.com/blog/2007/08/12/how-to-force-file-download-with-php/.
Adding all the headers recommended there and also using:
before any output - has helped. No more limitations observable. Files download completely even huge ones.
看起来您正在将整个文件加载到 RAM 中,然后再将其发送给收件人。您需要研究 PHP Streams,以便能够发送完整的文件内容,而不必先将其全部读入 RAM: http://php.net/streams
It seems like you're loading the entire file into RAM before sending it down to the recipient. You'll want to look into PHP Streams to be able to send the full file contents without having to read it all into RAM first: http://php.net/streams
还可能需要
set_time_limit(0);
also may need to
set_time_limit(0);
在 php.ini 中您将看到该设置。
我一时记不起选项名称,但我现在会查看 php.ini 并尝试找到它。
只需将其删除即可工作。
添加
好的,如果我错了,请有人纠正我,但是是吗?
Inside the php.ini you will see the setting.
I can't remember the option name off the top of my head, but I will look inside my php.ini now and try and find it.
Just remove it and it will work.
Added
Okay, someone please correct me if I am wrong, but is it
Apache 中的构建有 1 GB 限制,即使您尝试从根目录下载文件,也完全避免了 php。
There is 1 GB limit build in Apache, even if u'll try to download a file from he root directory, completely avoiding php.
花了我很长时间去调试。我认为这可能是 CloudFlare 背后的代理,标头丢失。结果我正在流式传输的文件达到了内存限制。
我做到了
Took me forever to debug. I thought it might be the proxy behind CloudFlare, Header Missing. Turned out the file I was streaming was hitting the memory limit.
I did