Upload multiple files as a zip file using php

How to upload multiple files as a zip file using php?

+85
php zip
Nov 18 '09 at 8:06
source share
5 answers

You can use the ZipArchive class to create a ZIP file and pass it to the client. Something like:

 $files = array('readme.txt', 'test.html', 'image.gif'); $zipname = 'file.zip'; $zip = new ZipArchive; $zip->open($zipname, ZipArchive::CREATE); foreach ($files as $file) { $zip->addFile($file); } $zip->close(); 

and pass it:

 header('Content-Type: application/zip'); header('Content-disposition: attachment; filename='.$zipname); header('Content-Length: ' . filesize($zipname)); readfile($zipname); 

The second line causes the browser to display the download window to the user and asks for the name filename.zip. The third line is optional, but some (mostly old) browsers have problems in certain cases without specifying the size of the content.

+160
Nov 18 '09 at 8:08
source share

This is a working example of creating ZIP files in PHP:

 $zip = new ZipArchive(); $zip_name = time().".zip"; // Zip name $zip->open($zip_name, ZipArchive::CREATE); foreach ($files as $file) { echo $path = "uploadpdf/".$file; if(file_exists($path)){ $zip->addFromString(basename($path), file_get_contents($path)); } else{ echo"file does not exist"; } } $zip->close(); 
+24
May 10 '13 at 10:58
source share

You can use the xip.lib.php library. zip.lib.php For example, see this article article

+3
Nov 18 '09 at 8:40
source share

Create a zip file, then upload the file by setting the header, read the contents of the zip and output the file.

http://www.php.net/manual/en/function.ziparchive-addfile.php

http://php.net/manual/en/function.header.php

+1
Nov 18 '09 at 8:09
source share

Are you ready to work with php zip lib, and can also use zend zip lib,

 <?PHP // create object $zip = new ZipArchive(); // open archive if ($zip->open('app-0.09.zip') !== TRUE) { die ("Could not open archive"); } // get number of files in archive $numFiles = $zip->numFiles; // iterate over file list // print details of each file for ($x=0; $x<$numFiles; $x++) { $file = $zip->statIndex($x); printf("%s (%d bytes)", $file['name'], $file['size']); print " "; } // close archive $zip->close(); ?> 

http://devzone.zend.com/985/dynamically-creating-compressed-zip-archives-with-php/

and there is also php pear lib for this http://www.php.net/manual/en/class.ziparchive.php

+1
Sep 06 '13 at 13:50
source share



All Articles