How to create a zip file and download multiple files using ZipArchive?

How to create a zip file and download multiple files using ZipArchive

In this PHP Tutorial, we are going to explain to you How to create a zip file and download multiple files using ZipArchive?

In this example, we will use open()  method to open or create any zip file and addFile() method is used to add files to archive and close() method is used to close the zip file safely.

ZipArchive is a PHP class that has many useful static functions to use. ZipArchive can be used in many scenarios in small to big websites.

We can use ZipArchive in a situation like if let’s suppose on our website we always have to show new information and for that, we always need to make sure our server disk should enough empty so that server space can be available for new data. So in that case we can zip the old files to a single file and store them somewhere on the other server with reference and manageability to save to disk space.

What is PHP zip and ZipArchive?

ZIP is an archive file format that supports lossless data compression. A ZIP file may contain one or more files or directories that may have been compressed. The PHP ZipArchive class can be used to zipping and unzipping.

ZipArchive class has many static functions that we can use as per our requirements. Refer the table – https://www.php.net/manual/en/class.ziparchive.php

From the above reference link, you can check the methods provided their and use them in the project as per your requirement. For example, if you need to add a new directory you can use ZipArchive::addEmptyDir method.

Use of ZipArchive

It might be need to install the class if it is not present. Installation for Linux users: In order to use these functions you must compile PHP with zip support by using the –enable-zip configure option.

Example:

You can use this example to download multiple files at the same time by creating zip file, you can simply add the downloading files in a zip with the help of PHP’s ZIP class.

In this we have created createZipArchive() function which takes two arguments. First one is files which we want to zip and the second one is file name.

<?php

/* create a compressed zip file */
function createZipArchive($files = array(), $destination = '', $overwrite = false) {

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

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

   if(count($validFiles)) {
      $zip = new ZipArchive();
      if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) == true) {
         foreach($validFiles as $file) {
            $zip->addFile($file,$file);
         }
         $zip->close();
         return file_exists($destination);
      }else{
          return false;
      }
   }else{
      return false;
   }
}

$fileName = 'myzipfile.zip';
$files = array('uploads/profile1.png', 'uploads/profile2.png');
$result = createZipArchive($files, $fileName);

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

?>

Conclusion –

ZipArchive class is very useful when you want to create a zip file. ZipArchive class also provided many inbuilt static methods which we can user further in the project.

If you have any query related to this please free to comment us below we would happy to help you guys.

You can also check How to enable gzip compression 

Spread the love