简体   繁体   中英

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)

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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