我使用此代码使用PHP压缩和下载文件 浏览器开始ZIP下载,但已损坏。 当我尝试打开浏览器时,说明格式文件未知或已损坏。
要解决的任何提示吗?
$files = array('1.png', '2.png');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=file.zip');
header('Content-Length: ' . filesize($zipfilename));
obclean();
flush();
readfile($zipname);
答案 0 :(得分:2)
问题出在这一行:
header('Content-Length: ' . filesize($zipfilename));
obclean();
将其更改为:
header('Content-Length: ' . filesize($zipname));
ob_clean();
答案 1 :(得分:1)
将此行更改为:
FROM:
$zip->addFile($file);
TO:
$zip->addFromString(basename($file), file_get_contents($file));