Zip Multiple Files and Download

This script will zip multiple files and  force download created zip file. This function required Zip Archive enable in your server.

<?php
function zipFilesDownload($file_names,$archive_file_name,$file_path){
$zip = new ZipArchive();
if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open <$archive_file_name>\n");
}
foreach($file_names as $files){
$zip->addFile($file_path.$files,$files);
}
$zip->close();

header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$archive_file_name");
header("Pragma: no-cache");
header("Expires: 0");
readfile("$archive_file_name");
exit;
}

$fileNames=array('files/file1.docx','files/file1.pdf');
$zip_file_name='myFile.zip';
$file_path=dirname(__FILE__).'/';
zipFilesDownload($fileNames,$zip_file_name,$file_path);
?>

Also Check:

Unzip Files in Web server

<?php
$zip = zip_open("moooredale.zip");
  if ($zip) {
   while ($zip_entry = zip_read($zip)) {
   $fp = fopen(zip_entry_name($zip_entry), "w");
   if (zip_entry_open($zip, $zip_entry, "r")) {
   $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
   fwrite($fp,"$buf");
   zip_entry_close($zip_entry);
   fclose($fp);
 }
}
zip_close($zip);
}
?>

Download Code Total Downloads: 660

Comments
comments powered by Disqus