使用php将多个文件下载为zip文件 [英] Download multiple files as a zip-file using php
本文介绍了使用php将多个文件下载为zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用php将多个文件下载为zip文件?
How can I download multiple files as a zip-file using php?
推荐答案
You can use the ZipArchive
class to create a ZIP file and stream it to the client. Something like:
$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.第三行是可选的,但某些(主要是较旧的)浏览器在某些情况下会出现问题,而未指定内容大小.
The second line forces the browser to present a download box to the user and prompts the name filename.zip. The third line is optional but certain (mainly older) browsers have issues in certain cases without the content size being specified.
这篇关于使用php将多个文件下载为zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文