//compressed FolderfunctionAddfiletozip ($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 that is read is a folder, the recursiveAddfiletozip ($path." /".$filename,$zip); }Else{//adding files to a Zip object /** AddFile (parameter 1, parameter 2) * Parameter 1: File full path (with file name) * Parameter 2: Custom new file name (without path directly added to Compress the package root directory, otherwise the corresponding directory will be automatically created in the compressed package) **/ $zip->addfile ($path." /".$filename,$filename); } } } @Closedir($path);}$zip=Newziparchive ();$dirname= '/uploads/text_dir ';//folders that need to be packaged$zip _name= ' download/'.$id.‘. Zip ';//the packaged file$res=$zip->open ($zip _name, Ziparchive::CREATE);if($res===TRUE) {Addfiletozip ($dirname,$zip);//invokes the method, operates on the root directory to be packaged, and passes the Ziparchive object to the method $zip->close ();//close the processed zip file//Browser download Header(' Content-type:application/zip '); Header(' Content-disposition:attachment; Filename= '.$zip _name); Header(' Content-length: '.filesize($zip _name)); ReadFile($zip _name);}Else{ Echo"<script>alert (' failed to create the package '); Location.reload ();</script>";Exit;}
PHP zipped folder with compression class