How to download multiple files as zip in php?
+11
A:
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');
$zip = new ZipFile;
$zip->open('file.zip', ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
and to stream it:
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=filename.zip');
header('Content-Length: ' . filesize($zipfilename));
readfile($zipname);
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.
cletus
2009-11-18 08:08:38
awesome info. Thanks!
a432511
2009-11-19 05:04:21
A:
Create a zip file, then download the file, by setting the header, read the zip contents and output the file.
http://www.php.net/manual/en/function.ziparchive-addfile.php
Priyank Bolia
2009-11-18 08:09:31
A:
You can use the xip.lib.php Class lib. zip.lib.php For example , refer to this article
Abdel Olakara
2009-11-18 08:40:35