简体   繁体   English

PHP 下载 ZIP 文件的脚本损坏文件

[英]PHP download script for ZIP files corrupting the file

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) $file等于我服务器上的文件位置(该位置在 public_html 目录之外)

When the file is downloaded, I open the file to find the zip archive corrupted.下载文件后,我打开文件发现 zip 存档损坏。 I can see a list of all the files in the zip but I can't open them.我可以看到 zip 中所有文件的列表,但我无法打开它们。 When I download the file through my FTP client, the archive is NOT corrupted.当我通过我的 FTP 客户端下载文件时,存档没有损坏。 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');我发现了问题:zlib 压缩损坏了文件,所以我添加了ini_set('zlib.output_compression', 'Off'); to the start of the script到脚本的开头

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.这里的问题是您没有发送正确的内容范围,因此报告给浏览器时文件大小不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM