- Package files into a zip format
- Features for downloading files
Key points of Analysis:
- Here I'm using PHP's own ziparchive class
A) We only need to new a Ziparchive object, then use the open method to create a zip file, and then use the AddFile method, the file will be packaged to write to the zip file just created, it is best to remember to close the object.
b) Note: When using the Open method, the second parameter, $flags, is optional, $flags used to specify how to handle the open zip file in four different cases
I. Ziparchive::overwrite always creates a new file, and if the specified zip file exists, it will overwrite the
II. ziparchive::create If the specified zip file does not exist, create a new
III. ziparchive::excl if the specified zip file exists, an error is given
iv.. Ziparchive::checkcon
$nid =intval($this_request->getparam (' nid '));$filelist = $thisModel->getoverenter ($nid);foreach($filelist as$f) {$row [' file '] =ROOT.'/public/upload/logo/'. $f [' Ne_file '];$row [' name '] = $f [' Es_name '];$ext =Explode('.',$f [' Ne_file ']);$row [' ext '] = $ext [1];$FILENAMEARR []= $row;} $name =Date(' Ymdhis ').'. zip ';$filename =ROOT."/public/upload/". $name; // the resulting file name (with path)$zip =NewZiparchive ();if(!file_exists($filename)) {$zip->open ($filename, ziparchive::CREATE);//Create an emptyZipfile for($i =0; $i <Count($FILENAMEARR); $i + +) {$zip->addfile ($fileNameArr [$i] [' file '], Iconv(' UTF-8 ',' GB2312 ', $FILENAMEARR [$i] [' name ']).'.'. $FILENAMEARR [$i] [' ext ']);} $zip->close ();//Open Fileif(file_exists($filename)) {$file =fopen($filename,"R");Header ("Content-type:application/octet-stream");Header ("Accept-ranges:bytes");Header ("Accept-length:".filesize($filename));Header ("content-disposition:attachment; Filename= ". $name);Echofread($file, filesize($filename));$buffer =1024x768; while(!feof($file)) {$file _data=fread($file,$buffer);Echo$file _data;}fclose($file);}Else{Echo"";}}unlink($filename);Exit();
The above describes the jar file package php Zip package download file, including the contents of the jar file packaging, I hope to be interested in PHP tutorial friends helpful.