$zip = new ZipArchive();
$filename = 'download.zip';
// 删一下原有文件,免得下载包多占空间
unlink($filename);
if ($zip->open($filename, ZipArchive::CREATE) !== TRUE) {
exit('无法创建文件');
}
$zip->addFile('a.png', '字母a.png');
$zip->addFile('b.png', '字母b.png');
$zip->close();
// 输出到浏览器下载
header('Content-Type: application/zip');
header('Content-Transfer-Encoding: Binary');
header('Content-disposition: attachment; filename='.$filename);
header('Content-Length: ' . filesize($filename));
// 这两行很重要,不然可能会出现解压的时候提示文件损坏
ob_clean();
flush();
readfile($filename);
PHP打包服务器文件下载到本地
最新推荐文章于 2025-09-08 15:12:05 发布
本文介绍了如何在PHP中利用ZipArchive类创建一个名为download.zip的压缩文件,添加a.png和b.png,然后通过HTTP头输出供浏览器下载。
1817

被折叠的 条评论
为什么被折叠?



