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
Here $the_folder is a array it contains the directories list.
Those are able to add the zip file
by using this
in case of adding / making the zip file any error occures it shows the
error message
To initialize the directories list like this
Here you simply mention the path of the directories.
And then to stream it.
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.
You can write like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $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' ; } |
1 | $za ->addDir( $value , basename ( $value )); |
1 | echo 'Could not create a zip archive' ; |
1 | $the_folder =[ 'pictures/' , 'movies/' ]; |
And then to stream it.
1 2 3 4 5 6 7 8 9 | 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 ); |
After download you can remove the zipped folder from your server
Full code
1 2 3 4 | if ( $delete_file_after_download ) { unlink( $zip_file_name ); }?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | <!-- * @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