PHP Create zip archive and Download Zip File using ZipArchive

PHP Create zip archive and Download Zip File using ZipArchive

In this Post We Will Explain About is PHP Create zip archive and Download Zip File using ZipArchive With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to How to create and download a Zip file with PHPExample

In this post we will show you Best way to implement Create zip archive from directory in PHP, hear for How to create a zip archive file for download in Laravel PHPwith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Read Also:  Laravel AJAX Delete Record From MySQL Table

First of all create a simple php file to archive all the data files and folder and save the all the data particuler destination path and here save successfully.!here live.jpg and live2.jpg 2 fils here available and php to create a ZipArchive simple and best way to zip created./* create simple a all the data compressed zip file using PHP */

create sure We have two image simple file should here available near like as a index.php file:

Read Also:  Eloquent Batch update multiple records using Laravel

1) live1.jpg

2) live2.jpg

<?php
function path_make_zip($files = array(), $save_path = '', $overwrite = false) {

   if(file_exists($save_path) && !$overwrite) { return false; }

   $check_files = [];
   if(is_array($files)) {
      foreach($files as $file) {
         if(file_exists($file)) {
            $check_files[] = $file;
         }
      }
   }

   if(count($check_files)) {
      $zip = new ZipArchive();
      if($zip->open($save_path,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
         return false;
      }

      foreach($check_files as $file) {
         $zip->addFile($file,$file);
      }

      $zip->close();
      return file_exists($save_path);
   }else{
      return false;
   }
}

$your_filename = 'live-archive.zip';
$all_filestozip = ['live1.jpg', 'live2.jpg'];
$result = path_make_zip($all_filestozip, $your_filename);

header("Content-Disposition: attachment; filename=\"".$your_filename."\"");
header("Content-Length: ".filesize($your_filename));
readfile($your_filename);

?>

Now And the last steps to We can run above PHP whole file and simple check it.

Read Also:  Download Extract Zip Archives using PHP cURL

Example

I hope you have Got What is How to create a zip archive file for download in Laravel PHP And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  How to exit a for loop or skip iteration in PHP
  2. Read Also:  What is the difference between Java and JavaScript?
  3. Read Also:  PHP Download Remote File from URL using CURL
  4. Read Also:  Download File from URL Using PHP
  5. Read Also:  Laravel 6 Eloquent Right JOIN Query Example Tutorial
  6. Read Also:  Get HTML source code of page with php - Scrape
  7. Read Also:  How to send Emails with MailGun in Laravel?
  8. Read Also:  Remove ALL white spaces from text Example
  9. Read Also:  Laravel 6 Inner Join Query Example - MySQL
  10. Read Also:  PHP is not recognized as an internal or external command