This article describes a simple way for PHP to create a zip archive. Share to everyone for your reference, as follows:
/* Creates a compressed zip file */function create_zip ($files = Array (), $destination = ", $overwrite = False) {//if the ZIP file already exists and overwrite is false, return False if (file_exists ($destination) &&! $overwrite) {retur n false; }//vars $valid _files = Array (); If files were passed in ... if (Is_array ($files)) {//cycle through each file foreach ($files as $file) {//make sure the file exists if (File_exists ($file)) {$valid _files[] = $file; }}}//if we have good files ... if (count ($valid _files)) {//create the archive $zip = new ziparchive (); if ($zip->open ($destination, $overwrite? Ziparchive::overwrite:ziparchive::create)!== true) {return false; }//add the files foreach ($valid _files as $file) {$zip->addfile ($file, $file); }//debug//echo ' The zip archive contains ', $zip->numfiles, ' files with a status of ', $zip->status; Close the zip--done! $zip->close (); Check to make sure the file exists return file_exists ($destination); } else {return false; }}
How to use:
$files _to_zip = Array ( ' preload-images/1.jpg ', ' preload-images/2.jpg ', ' preload-images/5.jpg ', ' Kwicks/ringo.gif ', ' rod.jpg ', ' reddit.gif ');//if true, good; if False, zip creation Failed$result = Create_zip ( $files _to_zip, ' my-archive.zip ');
I hope this article is helpful to you in PHP programming.
The above describes the PHP simple way to create a zip file, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.
Related articles:
Generate Zip Compressed files from PHP, support file and archive path lookup
How to use PHP to generate zip compressed files with detailed code
PHP implementation of online decompression zip archive