ZIP 文件的 PHP 下载脚本会损坏文件
这是我的代码:
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$filename.'.zip');
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: ' . filesize($file));
ob_clean();
flush();
@readfile($file);
exit;
$file
等于我的服务器上的文件位置(该位置位于 public_html 目录之外)
下载文件后,我打开文件发现 zip 存档已损坏。我可以看到 zip 中所有文件的列表,但无法打开它们。当我通过 FTP 客户端下载文件时,存档没有损坏。我在脚本之前或之后没有任何空格。什么可能导致此文件损坏?
编辑:
我发现问题:zlib 压缩损坏了文件,因此我将 ini_set('zlib.output_compression', 'Off');
添加到脚本的开头
Here is my code:
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$filename.'.zip');
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: ' . filesize($file));
ob_clean();
flush();
@readfile($file);
exit;
$file
is equal to the file location on my server (the location is outside public_html directory)
When the file is downloaded, I open the file to find the zip archive corrupted. I can see a list of all the files in the zip but I can't open them. When I download the file through my FTP client, the archive is NOT corrupted. I do not have any whitespace before or after the script. What could be causing this file corruption?
Edit:
I have found the problem: zlib compression was corrupting the files so I added ini_set('zlib.output_compression', 'Off');
to the start of the script
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题是您没有发送正确的内容范围,因此当向浏览器报告时文件大小不正确。
The problem here is you are not sending a correct content range, therefore the file size is not correct when it is reported to the browser.