PHP Create zip archive and Download Zip File using ZipArchive

Mysql Mysqli php Programming

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!.

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:

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.

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.   Rendering Lists in VueJs Example
  2.   Download File from URL Using PHP
  3.   How To Add Flash Messages in Laravel 6 Session?
  4.   get_post_meta in WordPress Example
  5.   PHP Auto image resizing Script
  6.   what does pairwise distinct mean?
  7.   How to Make a Redirect to page in PHP?
  8.   PHP Download Remote File from URL using CURL
  9.   Download Extract Zip Archives using PHP cURL
  10.   Laravel 6 Install Constant Contact - PHP Examples

Leave a Reply

Your email address will not be published. Required fields are marked *