如何使用php下载zip文件后修复“无效文件”错误?

时间:2016-08-24 12:57:09

标签: php zip

我正在尝试使用php下载一个zip文件。下载工作正常但在提取zip时它说无效文件。请帮助我,检查下面的代码:

apache_setenv('no-gzip', 1);
ini_set('zlib.output_compression', 0);

$filename = "sliced Images.zip";    
$upload_dir = wp_upload_dir();

$filepath = $upload_dir['basedir'];
//echo $filepath.'/'.$filename;
//echo filesize($filepath.'/'.$filename);
//die;

// http headers for zip downloads
ob_start();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: Binary");

header("Content-Disposition: attachment; filename=\"".$filename."\"");

header("Content-Length: ".filesize($filepath.'/'.$filename));
ob_end_flush();
@readfile($filepath.'/'.$filename);

enter image description here

1 个答案:

答案 0 :(得分:2)

使用以下代码解决上述问题:

header('Content-Description: File Transfer');
header("Content-Type: application/zip");
header('Content-Disposition: attachment; filename="'.basename($pdf_path).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($pdf_path));
ob_clean();
readfile($pdf_path);