Last active
December 2, 2015 21:25
-
-
Save khoand0000/3d9f7ae8235e00742896 to your computer and use it in GitHub Desktop.
create zip file from multi-files
ref: http://stackoverflow.com/questions/1754352/download-multiple-files-as-zip-in-php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$files = array('readme.txt', 'test.html', 'image.gif'); | |
$zipname = 'file.zip'; | |
$zip = new ZipArchive; | |
//$zip->open($zipname, ZipArchive::CREATE); // the mode will append more files to existing file | |
$zip->open($zipname, ZipArchive::OVERWRITE); // always create new file even file is existing | |
foreach ($files as $file) { | |
// $zip->addFile($file); // original code if you want to keep original path of $file when extracting zipped file, means that $zip will create hierarchy folders contain the file | |
$zip->addFile($file, basename($file)); // changed, if you want to zip all files in single folder | |
} | |
$zip->close(); | |
header('Content-Type: application/zip'); | |
header('Content-disposition: attachment; filename='.$zipname); | |
header('Content-Length: ' . filesize($zipname)); | |
readfile($zipname); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment