PHP example code analysis: compress a folder into ZIP_PHP tutorial

WBOY
Release: 2016-07-21 14:55:39
Original
801 people have browsed it

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 ClipboardLiehuo.Net CodesQuoted 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/

www.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...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!