<?php function Addfiletozip ($path, $zip) {$handler = Opendir ($path); Opens the current folder specified by $path. while (($filename = Readdir ($handler))!== false) {if ($filename! = "." && $filename! = "...") {//Folder file name is '. ' and '. ', do not operate on them if (Is_dir ($path. "/" . $filename) {//If an object being read is a folder, the recursive Addfiletozip ($path. "/" . $filename, $zip); } else {//Add the file to the Zip object $zip->addfile ($path. "/" . $FILENAME); }}} @closedir ($path); } $zip = new ziparchive (); $file =date ("YMD"). ". Zip "; fopen ($file, ' W '), or//In Windows environment can omit this sentence, but in the LIUNX environment, it is best to add, because in some liunx environment Ziparchive::create
Method cannot automatically create files, will Causes file compression to fail.
if ($zip->open ($file, ziparchive::create) ===true) { if (file_exists ($file)) { addfiletozip (' Test ', $zip); Invokes the method, operates on the root directory to be packaged, and passes the Ziparchive object to the method }else{ echo "file does not exist"; } Close the processed zip file if ($zip->close ()) { return true; } else{ return false; } } else { echo "failed"; }
PHP file Compression zip extension