ThinkPHP package local files into zip files to download _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags ziparchive
ThinkPHP package local files into zip files for download. First, set FileToZip. put the class file in the ThinkPHPExtendLibraryORGUtil folder, FileToZip. class. php is a zip download class. the detailed code is as follows: php *** zip download class file **. first, set FileToZip. put the class file in the ThinkPHP/Extend/Library/ORG/Util/folder, FileToZip. class. php is a zip download class. the detailed code is as follows:

<? Php/*** zip download class file * traversal Directory, packaged into zip format */class traverseDir {public $ currentdir; // Current Directory public $ filename; // file name public $ fileinfo; // used to save all file names and directory names in the current directory and the file size public $ savepath; public function _ construct ($ curpath, $ savepath) {$ this-> currentdir = $ curpath; // returns the current directory $ this-> savepath = $ savepath; // return to the current directory} // traverse the public function scandir ($ filepath) {if (is_dir ($ filepath) {$ 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(inclusavepath.20.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 ', $ savepath ); // download the file $ dw-> getfiles (); unlink(~savepath.20.zipname.'.zip '); // after the download is complete, delete the file.}/*** download file **/class download {protected $ _ filename; protected $ _ filepath; protected $ _ filesize; // file size protected $ savepath; // File Size public function _ construct ($ filename, $ savepath) {$ this-> _ filename = $ filename; $ This-> _ filepath = $ savepath. $ 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 two digits after the decimal point} // download the object function public function getfiles () {// check whether the object exists if (file_exists ($ this-> _ filepath) {// Open File $ file = fopen ($ this-> _ filepath, "r"); // The Returned file type Header ("Content-type: application/octet-stream "); // return the Header ("Accept-Ranges: bytes") according to the byte size; // return the 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 data back $ 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 ";}}
 

The code for loading and downloading a zip file in ThinkPHP is as follows:

Import ('org. util. fileToZip '); // introduce the zip download class file FileToZip // package and download $ handler = opendir ($ cur_file); // $ cur_file file directory $ download_file = array (); $ I = 0; while ($ filename = readdir ($ handler ))! = False) {if ($ filename! = '.' & $ Filename! = '.. ') {$ Download_file [$ I ++] = $ filename;} closedir ($ handler); $ scandir = new traverseDir ($ cur_file, $ save_path ); // $ save_path zip package file directory $ scandir-> tozip ($ download_file );

Unzip php/*** zip download class file * times...

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.