Use PHP to package zip and download features

Source: Internet
Author: User
Tags ziparchive
This article mainly introduces the PHP implementation package zip and download function, code simple and easy to understand, very good, with a certain reference value, need to refer to the friend

The specific code looks like this:

$file _template = fcpath. ' Canddata/cand_picture.zip ';//before this you must create a new empty zip package (must exist) in your project directory $downname = $card. Zip ';//the ZIP file name you are about to pack $file_name = fcpath. ' canddata/'. $card. Zip ';//pack your zip after the directory $result = copy ($file _template, $file _name);//The original project directory exists zip copy a new to another directory and rename (can be in the original directory) $zip = New Ziparchive ();//Create an object if ($zip->open ($file _name, ziparchive::create) = = = TRUE) {//Open the empty zip package after you copy $zip  Addemptydir ($card);//Create an empty folder in the Zip package, return TRUE on success, or return FALSE on Failure//The following is my scene business processing, can be processed according to my own scene (I will pack all the pictures) $i = 1; foreach ($cand _photo as $key 3 = $value 3) {$file _ext = explode ('. ', $value 3[' cand_face '));//Gets the suffix of the picture $zip->ad Dfromstring ($card. ' /'. $card. ' _ '. $i. '.  $file _ext[3], file_get_contents ($value 3[' cand_face '));//(Rename the image, get to the picture of the binary stream) $i + +;  } $zip->close ();   $FP =fopen ($file _name, "R");   $file _size=filesize ($file _name);//The Header ("Content-type:application/octet-stream") to get the file's byte//download file to use;   Header ("Accept-ranges:bytes");  Header ("Accept-length:". $file _size); Header ("ConTent-disposition:attachment;   Filename= $downname "); $buffer = 1024; Sets the number of bytes read at a time, each time it is read, the output data (that is, returned to the browser) $file _count=0; Total bytes read//Return data to the browser if the download is complete, stop the output, if it is not downloaded, it will always be output.          Determine whether the download completed while (!feof ($fp) && $file _count< $file _size) {$file _con=fread ($fp, $buffer) based on the size of the file's bytes;          $file _count+= $buffer;      echo $file _con;   } fclose ($FP);   After the download is complete, delete the compressed package, the temporary folder if ($file _count >= $file _size) {unlink ($file _name); }}

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.