ThinkPHP package local files into zip files for download

Source: Internet
Author: User
Tags ziparchive

First, put the FileToZip. class file in the ThinkPHP/Extend/Library/ORG/Util/folder, and FileToZip. class. php is the 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 <count ($ items); $ I ++) {$ zip-> 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 '); // delete the file after the download is complete}/*** download file **/class download {protected $ _ filename; protected $ _ filepa Th; 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 (files Ize ($ this-> _ filepath)/(1024*1024), 2); // remove two digits after the decimal point} // public function getfiles () {// check whether the file exists if (file_exists ($ this-> _ filepath) {// open the file $ file = fopen ($ this-> _ filepath, "r"); // returned file type Header ("Content-type: application/octet-stream"); // return Header ("Accept-Ranges: bytes "); // The Header (" Accept-Length :". filesize ($ this-> _ filepath); // the pop-up dialog box for the client, the corresponding file name Header ("Content-Disposition: atta Chment; 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> ";}}}

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 );

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.