How to open the ziparchive extension, please do your own Baidu.
Directly on the code
A single file is inserted into the ZIP package function addfiletozip ($path, $zip) {$handler = opendir ($path); // Opens the current folder specified by $path. /* Looping all files and folders under the Read folder where $filename = readdir ($handler) is assigned the read file name to $filename each time the loop is looped, so that it is not trapped in a dead loop, so Filename !== false. Be sure to use!==, because if a file name is called ' 0 ', or if some are considered false by the system, the Loop */while (($filename = readdir ($handler)) will be stopped with! =. !== 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, Then recursive addfiletozip ($path . "/" . $filename, $zip);} else { //Add the file to the Zip object $zip->addfile ($path . "/" . $filename);}} @closedir ($path);} /* * @creates a compressed zip file The function of compressing multiple files into a zip file * @ $files Array type instance Array ("1.jpg", "2.jpg"); & nbsp; * @destination Path to the destination file such as "C:/androidyue.zip" * @ $overwrite Whether to overwrite the same file */ function create_zip as the destination file ($files = array (), $destination = ", $overwrite = false) { //if the zip file already exists and overwrite is false, return false //if the zip file already exists and is set to not override the return false if (file_exists ($destination) && ! $overwrite) { return false; } //vars $valid _files = array (); //if files were passed in ... //get to a real valid filename 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 there is a real valid file if ( Count ($valid _files)) { //create the archive $zip = new ziparchive (); //Open File Overwrite if file already exists, if not, create if ($zip->open ($destination, $overwrite ? ziparchive::o verwrite : ziparchive::create) !== true) { return false; } //add the files //adding files to a compressed file if ($zip->open ($destination, ziparchive::overwrite) === true) { foreach ($valid _files as $file) { &nbsP; addfiletozip ($file, $zip); // Invokes the method, operates on the root directory to be packaged, and passes the Ziparchive object to the method } } //debug //echo ' the zip archive contains ', $zip->numfiles, ' files with a status of ', $zip->status; //close the zip -- done! //closing Files $zip->close (); //check to make sure the file exists //detect if file exists return file_exists ($destination); }else{ //if no real valid file is returned False return false; } }
Single File Download instance:
$filename = '. /download/zipfilename.zip '; The path of the compressed file $zip = new Ziparchive (), if ($zip->open ($filename, ziparchive::overwrite) = = = TRUE) {addfiletozip (": /folder ", $zip); Call the method, manipulate the root directory to be packaged, and pass the Ziparchive object to the method $zip->close (); Close the processed zip file}header ("Cache-control:public"); Header ("Content-description:file Transfer"); Header (' content-disposition:attachment; Filename= '. basename ($filename)); FileName header ("Content-type:application/zip"); Header in zip format ("content-transfer-encoding:binary"); Tell the browser that this is the header of the binary file (' content-length: '. FileSize ($filename)); Tell the browser that the file size @readfile ($filename);
Multiple file package Download instance:
$filsarr = Array (".. /folder1 ",". /folder2 ",". /folder5 ",". /folder11 "); You can select the contents of each directory $filename = '. /download/zipfilename.zip '; The path of the compressed file Create_zip ($filsarr, $filename); Header ("Cache-control:public"); Header ("Content-description:file Transfer"); Header (' content-disposition:attachment; Filename= '. basename ($filename)); FileName header ("Content-type:application/zip"); Header in zip format ("content-transfer-encoding:binary"); Tell the browser that this is the header of the binary file (' content-length: '. FileSize ($filename)); Tell the browser that the file size @readfile ($filename);
PHP Learning notes-Very useful PHP code snippets (1)