如何使用php将多个文件下载为zip文件?
发布于 2009-11-18 16:08:39
您可以使用ZipArchive
类创建ZIP文件并将其流式传输到客户端。类似于:
$files = array('readme.txt', 'test.html', 'image.gif');
$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='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
第二行强制浏览器向用户显示一个下载框并提示名称filename.zip。第三行是可选的,但某些(主要是较旧的)浏览器在未指定内容大小的情况下会出现问题。
发布于 2013-05-10 18:58:38
这是一个用PHP制作ZIPs的工作示例:
$zip = new ZipArchive();
$zip_name = time().".zip"; // Zip name
$zip->open($zip_name, ZipArchive::CREATE);
foreach ($files as $file) {
echo $path = "uploadpdf/".$file;
if(file_exists($path)){
$zip->addFromString(basename($path), file_get_contents($path));
}
else{
echo"file does not exist";
}
}
$zip->close();
发布于 2009-11-18 16:09:32
创建一个zip文件,然后下载该文件,通过设置头部,读取zip内容并输出该文件。
http://www.php.net/manual/en/function.ziparchive-addfile.php
https://stackoverflow.com/questions/1754352
复制相似问题