Skip to content

Instantly share code, notes, and snippets.

@khoand0000
Last active December 2, 2015 21:25
Show Gist options
  • Save khoand0000/3d9f7ae8235e00742896 to your computer and use it in GitHub Desktop.
Save khoand0000/3d9f7ae8235e00742896 to your computer and use it in GitHub Desktop.
$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