How to download the multiple directories as a zip file in PHP

If you want zip the multiple directories and make available to download that zip file. You can use the ZipArchive class to create a ZIP file and stream it to the client.
You can write like this
 $za = new ZipArchive;
$res = $za->open($zip_file_name, ZipArchive::CREATE);
if($res === TRUE) 
{
    foreach ($the_folder as $key => $value)
    {
        $za->addDir($value, basename($value));
    }
    $za->close();
}
else  
{ 
    echo 'Could not create a zip archive';
}
Here $the_folder is a array it contains the directories list. Those are able to add the zip file by using this
 
$za->addDir($value, basename($value));
in case of adding / making the zip file any error occures it shows the error message
 
echo 'Could not create a zip archive';
To initialize the directories list like this
 
$the_folder =['pictures/','movies/'];
Here you simply mention the path of the directories.
And then to stream it.
 
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private", false);
    header("Content-Type: application/zip");
    header("Content-Disposition: attachment; filename=" . basename($zip_file_name) . ";" );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: " . filesize($zip_file_name));
    readfile($zip_file_name);
The sixth line forces the browser to present a download box to the user and prompts the name filename.zip. The eight line is optional but certain (mainly older) browsers have issues in certain cases without the content size being specified.
After download you can remove the zipped folder from your server
 
if($delete_file_after_download)
{
    unlink($zip_file_name);
}?>
Full code
 
 <!--  
 * @author TechiesBadi  
 * How to download the multiple directories as a zip file  
 -->  
 <?php  
 $the_folder =['pictures/','movies/'];  
 $zip_file_name = 'download.zip';  
 $download_file= true;  
 $delete_file_after_download= true;   
 class FlxZipArchive extends ZipArchive {  
   /** Add a Dir with Files and Subdirs to the archive **/  
   public function addDir($location, $name) {  
     $this->addEmptyDir($name);  
     $this->addDirDo($location, $name);  
    } // EO addDir;  
   /** Add Files & Dirs to archive **/  
   private function addDirDo($location, $name) {  
     $name .= '/';  
     $location .= '/';  
     // Read all Files in Dir  
     $dir = opendir ($location);  
     while ($file = readdir($dir))  
     {  
       if ($file == '.' || $file == '..') continue;  
       // Rekursiv, If dir: FlxZipArchive::addDir(), else ::File();  
       $do = (filetype( $location . $file) == 'dir') ? 'addDir' : 'addFile';  
       $this->$do($location . $file, $name . $file);  
     }  
   } // EO addDirDo();  
 }  
 $za = new FlxZipArchive;  
 $res = $za->open($zip_file_name, ZipArchive::CREATE);  
 if($res === TRUE)   
 {  
   foreach ($the_folder as $key => $value)  
   {  
     $za->addDir($value, basename($value));  
   }  
   $za->close();  
 }  
 else   
 {   
   echo 'Could not create a zip archive';  
 }  
 if ($download_file)  
 {  
   ob_get_clean();  
   header("Pragma: public");  
   header("Expires: 0");  
   header("Cache-Control: must-revalidate, post-check=0, pre-check=0");  
   header("Cache-Control: private", false);  
   header("Content-Type: application/zip");  
   header("Content-Disposition: attachment; filename=" . basename($zip_file_name) . ";" );  
   header("Content-Transfer-Encoding: binary");  
   header("Content-Length: " . filesize($zip_file_name));  
   readfile($zip_file_name);  
 }  
 if($delete_file_after_download)  
 {  
   unlink($zip_file_name);  
 }?> 

* If you like this post please don’t forget to subscribe Techies Badi - programming blog for more useful stuff


EmoticonEmoticon