PHP 动态 ZIP 下载为空白
看来我成功创建了一个动态 zip 存档(filesize 和 file_exists 都返回预期值),但是当我尝试实际下载它时,我收到一个空的 ZIP 文件。 奇怪的是,readfile 和 fread 都会出现此错误。这是我的代码
$filename = $zip;
$handle = fopen($filename, 'r');
if ($handle === false)
{
die('Could not read file "' . $filename . '"');
}
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="fsdownload.zip"');
header('Cache-Control: private');
while (!feof($handle))
{
echo fread($handle, 8192);
}
fclose($handle);
,这适用于 zip 文件 < 10MB。对可能出现的问题有什么想法吗?
It seems I successfully create an on-the-fly zip archive (filesize and file_exists both return the expected values) but when I attempt to actually download it, I receive an empty ZIP file.
Curiously enough this error occurs with both readfile and fread. This is my code
$filename = $zip;
$handle = fopen($filename, 'r');
if ($handle === false)
{
die('Could not read file "' . $filename . '"');
}
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="fsdownload.zip"');
header('Cache-Control: private');
while (!feof($handle))
{
echo fread($handle, 8192);
}
fclose($handle);
This works fine for zip-Files < 10 MB. Any thoughts on what the problem might be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了避免消耗太多内存,您可以使用 ZipStream 或 PHPZip,这将发送将压缩文件即时压缩到浏览器,分成块,而不是在 PHP 中加载整个内容,然后发送 zip 文件。
这两个库都是不错且有用的代码。一些细节:
相关SO问题:
To avoid consuming too much memory, you can use ZipStream or PHPZip, which will send zipped files on the fly to the browser, divided in chunks, instead of loading the entire content in PHP and then sending the zip file.
Both libraries are nice and useful pieces of code. A few details:
Related SO questions:
问题是 PHP 限制。检查内存和执行时间限制。
The problem is a PHP limit. Check for memory and execution time limits.