Batch Zip Download


/ Published in: PHP
Save to your folder(s)

Code for downloading multiple files as Zip file


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. if (isset($_POST)) {
  4. $filesToSend = $_POST['files'];
  5. if (count($filesToSend) > 0) {
  6. $zipname = time().".zip";
  7. $zip = new ZipArchive();
  8. $res = $zip->open($zipname, ZipArchive::CREATE);
  9.  
  10. if ($res === TRUE) {
  11.  
  12. foreach ($filesToSend as $file) {
  13.  
  14. $zip->addFile($file,pathinfo($file, PATHINFO_BASENAME));
  15. }
  16. } else {
  17.  
  18. echo 'failed, code:' . $res;
  19. }
  20.  
  21. $zip->close();
  22.  
  23. header("Cache-Control: no-store, no-cache, must-revalidate");
  24. header("Cache-Control: post-check=0, pre-check=0", false);
  25. header("Pragma: no-cache");
  26. header("Content-type: application/zip");
  27. header('Content-length: ' . filesize($zipname));
  28. header('Content-disposition: attachment; filename=' . basename($zipname));
  29. flush();
  30. readfile($zipname);
  31. @unlink($zipname);
  32. } else {
  33. echo "nothing new";
  34. }
  35. }
  36. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.