Generating zip in php is actually very simple and can be completed in the following 2 steps.
1. Please download the zip.php tool class I prepared first, unzip it after downloading, and put the files inside into the corresponding directory. I placed it in the include folder under the virtual directory.
2. Add the following code to your php file
require_once "./include/zip.php";
$zip = new PHPZip();
//$zip -> createZip("Directory address of the folder to be compressed", "Compressed file name.zip"); //Only generated and not automatically downloaded
$zip -> downloadZip( "Directory address of the folder to be compressed", "Compressed file name.zip"); //Automatic download
Example: You can refer to the pseudocode below to see a specific usage scenario:
Copy to ClipboardQuoted content:
[www.bkjia.com]
require_once "./include/zip.php";
if (!$download) {
exit();
}
set_time_limit(60);
$ tmpManager = new TmpManager(); //Assume we have a class to complete the following operations
$tempfolder = array();
$tempfile = array();
//Assume we are Download the selected folder or file by selecting the checkbox on the page and package it together
for($i = 0;$i < $checkboxnum;$i++) {
$value = ${"select ".$i};
if ($value != '') {
$this_type = substr($value, 0, 1);
$this_id = substr($value, 1);
//Handle folders and files separately
if ($this_type == 'd') {
$tempfolder[] = $this_id;
}
elseif ($this_type = = 'f') {
$tempfile[] = $this_id;
}
}
}
@mkdir($tempdir);
$curtempdir = "$tempdir/".$userid; //Different users operate in different temporary folders
if (file_exists($curtempdir)) {
$tmpManager->DeleteDir($curtempdir) ; //Delete old folder
}
if (sizeof($tempfolder) > 0 || sizeof($tempfile) > 0) {
mkdir($curtempdir, 0777) ; //If there are files or folders to be packaged, recreate the folder
}
if (sizeof($tempfile) > 0) {
$tmpManager->CopyFile($ tempfile,$curtempdir); //Copy the file to be downloaded to the created folder
}
if (sizeof($tempfolder) > 0) {
$tmpManager->CopyFolder( $tempfolder,$curtempdir); //Copy the folder to be downloaded to the created folder
}
$zip = new PHPZip();
$zip -> downloadZip($curtempdir , "file_".date('Ymd').".zip"); //Package and download
Reprinted from: http://www.cnblogs.com/xiaosuo/
http://www.bkjia.com/PHPjc/364396.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364396.htmlTechArticleGenerating a zip in php is actually very simple and can be completed in the following 2 steps. 1. Please download the zip.php tool class I prepared first, unzip it after downloading, and put the files inside into the corresponding directory. I am...