First, place the Filetozip.class file in the thinkphp/extend/library/org/util/folder, FileToZip.class.php the zip download class with the following detailed code:
<?php/** * Zip download class file * Traverse directory, packaged into ZIP Format/class traversedir{public $currentdir;//current directory public $filename;//File name Publ
IC $fileinfo;//To save all file names and directory names and file size public $savepath under the current directory; The Public function __construct ($curpath, $savepath) {$this->currentdir= $curpath;//returns the current directory $this->savepath=$
savepath;//returns the current directory}//Traversal directory Public Function Scandir ($filepath) {if (is_dir) {$arr =scandir ($filepath);
foreach ($arr as $k => $v) {$this->fileinfo[$v][]= $this->getfilesize ($v);
}}else {echo "<script>alert (' current directory is not a valid directory ');</script>";
/** * Returns the size of the file * * @param string $filename filename * @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 ($savepath. $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 file $dw->getfiles (); Unlink ($savepath. $zipname. Zip ');
Delete after download}}/** * Download file * * */class download{protected $_filename;
protected $_filepath; Protected $_filesize;//File Size protected $savepath//File size public function __construct ($filename, $savepath) {$this->_f
Ilename= $filename;
$this->_filepath= $savepath. $filename;
//Get filename public Function GetFileName () {return $this->_filename;
//Get file path (including filename) public function GetFilePath () {return $this->_filepath; //Get File size public function GetFileSize () {return $this->_filesize=number_format filesize ($this->_filepath)/(
1024*1024), 2);////go to two-bit after decimal point////download file function Public Function GetFiles () {//Check file exists if (file_exists ($this->_filepath)) {
Open File $file = fopen ($this->_filepath, "R"); The returned file type Header ("Content-typE:application/octet-stream ");
Returns the Header ("Accept-ranges:bytes") by byte size;
Returns the size Header of the file ("Accept-length:". FileSize ($this->_filepath)); Here on the client's pop-up dialog box, the corresponding filename Header ("content-disposition:attachment;
Filename= ". $this->_filename);
Before modifying, a one-time transfer of data to the client echo fread ($file, FileSize ($this->_filepath)); After modification, only 1024 bytes of data are transmitted at a time to the client//client loopback data $buffer =1024;////Determine whether the file is read while (!feof ($file)) {//Read the file into memory $fil
E_data=fread ($file, $buffer);
Each time 1024 bytes of data are echoed to the client echo $file _data;
} fclose ($file);
}else {echo "<script>alert (' Sorry, the file you are downloading does not exist ');</script>"; }
}
}
The functional code for loading the ZIP download class FileToZip.class.php and implementing the local file package download in thinkphp is as follows:
Import (' ORG. Util.filetozip ');//introduce zip download class file Filetozip
//package 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);