Package files, download ZipArchive files that come with PHP, and download the packaged files.

Source: Internet
Author: User
Tags ziparchive
Package files, download ZipArchive files that come with PHP, and download the packaged files. Summary:

  1. Four headers () are required for downloading files using PHP. For more information, see my other blog post:Detailed process analysis on how PHP implements the download function
  2. When calculating the file size, you do not need to open the file first. you can see through filesize ($ filename) that if you need to open the file first, filesize may be in this format: filesize ($ filehandle)
  3. To send data back to the client, remember to setBufferTo specify how much data is output to the client each time, for example, $ buffer = 1023. If this parameter is not specified, the entire file will be written into the memory, and the data will be transferred to the client at one time.
  4. The feof () function can be used to determine whether the file to be read has been read. if the file has not been read, continue to read the file ($ file_data = fread ()), and send the data back to the client (echo $ file_data)
  5. After each download is complete, the client will refresh and explain that the data is written to a temporary file every time. after all the downloads are complete, all the data will be reintegrated.
  6. Here, I use absolute paths. the advantage of absolute paths is that they are adaptable and more efficient than relative paths (eliminating the need to find files)
Technical points analysis:

  1. Package files in zip format
  2. Object downloading

Key points:

  1. Here I use the ZipArchive class that comes with php.

A) we only need to create a new ZipArchive object, create a zip file using the open method, and then use the addFile method to write the file to be packaged into the zip file we just created, you 'd better remember to close this object.

B) Note: When using the open method, the second parameter $ flags is optional. $ flags is used to specify the processing method for opened zip files. There are four cases.

I. ZIPARCHIVE: OVERWRITE always creates a new file. if the specified zip file exists, it will OVERWRITE it.

Ii. ZIPARCHIVE: CREATE if the specified zip file does not exist, CREATE

Iii. ZIPARCHIVE: EXCL if the specified zip file exists, an error is returned.

Iv. ZIPARCHIVE: CHECKCONS

File download process:

Work on the server:
-------------------------------------------
The browser of the client sends a request to process the downloaded php file.
Note: Any operation must first be written to the memory. whether it is a video, audio, or text file, it must first be written to the memory.
In other words, it is essential to read files on the server into the memory of the server. (Note: I add double quotation marks to the server, it mainly indicates that the operations of this series are completed on the server ).

Since you want to write the file into the memory, you must first open the file
So here we need three File operation functions:
I. fopen ($ filename, $ mode)
II. fread (int $ handle, int $ length)
III. fclose (resource $ handle)

---------------------------------------
Client-side work:
---------------------------------------
Then, how can we transmit the file information that already exists in the server memory to the client?
The answer is that through the header () function, the client will know how to process the file, whether to save or open the file, and so on.

Shows the final effect:

 Currentdir = getcwd (); // returns the current directory} // traverses the public function scandir ($ filepath) {if (is_dir ($ filepath) Directory )) {$ arr = scandir ($ filepath); foreach ($ arr as $ k => $ v) {$ this-> fileinfo [$ v] [] = $ this-> getfilesize ($ v );}} else {echo "script" alert ('the current directory is not a valid directory'); script ";}} /*** return file size ** @ param string $ filename File Name * @ return file size (KB) */public function getfilesize ($ fname) {return filesize ($ fname) /1024;}/*** compressed file (Zip format) */public function tozip ($ items) {$ zip = new ZipArchive (); $ zipname = date ('ymdhis ', time (); if (! File_exists ($ zipname) {$ zip-> open(your zipname.'.zip ', ZipArchive: OVERWRITE); // create an empty zip file for ($ I = 0; $ I
 
  
AddFile ($ this-> currentdir. '/'. $ items [$ I], $ items [$ I]);} $ zip-> close (); $ dw = new download(+zipname.'.zip '); // download the file $ dw-> getfiles (); unlink($zipname.'.zip '); // delete the file after the download is complete }}?>
 



 _ Filename = $ filename; $ this-> _ filepath = dirname (_ FILE __). '/'. $ filename;} // get the file name public function getfilename () {return $ this-> _ filename;} // get the file path (including the file name) public function getfilepath () {return $ this-> _ filepath;} // Obtain the file size. public function getfilesize () {return $ this-> _ filesize = number_format (filesize ($ this-> _ filepath) // (1024*1024), 2); // remove the two digits after the decimal point} // download the object function public function getfiles () {// check whether the object exists if (fil E_exists ($ this-> _ filepath) {// open the file $ file = fopen ($ this-> _ filepath, "r "); // returned file type Header ("Content-type: application/octet-stream"); // return the Header ("Accept-Ranges: bytes") in bytes "); // response file size Header ("Accept-Length :". filesize ($ this-> _ filepath); // The pop-up dialog box for the client, the corresponding file name Header ("Content-Disposition: attachment; filename = ". $ this-> _ filename); // Before modification, transmit data to the client echo fread ($ file, filesize ($ this-> _ filepath) at a time )); // After modification, only 1024 bytes of data are transmitted at a time to the client // send back data to the client $ buffer = 1024; /// determine whether the file has been read while (! Feof ($ file) {// read the file into the Memory $ file_data = fread ($ file, $ buffer ); // 1024 bytes of data echo $ file_data;} fclose ($ file);} else {echo "script" alert ('sorry, the file you want to download does not exist. '); script ";}}?>



Page code:

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.