PHP Instance Code analysis: Compress folders into zip_php tutorial

Source: Internet
Author: User
Creating a Zip in PHP is really simple, as long as the following 2 steps can be completed.

1. Please download my prepared zip.php tool class, download and unzip, put the files inside the corresponding directory, I was placed in the virtual directory in the Include folder.

2. Add the following code to your PHP file

Require_once "./include/zip.php";

$zip = new Phpzip ();
$zip-Createzip ("folder directory address to compress", "compressed file name. zip"); Generate non-automatic downloads only
$zip-Downloadzip ("folder directory address to compress", "compressed file name. zip"); Automatic download

Example: You can refer to the following pseudo-code to see a specific usage scenario:

Copy to ClipboardWhat to refer to: [www.bkjia.com]Require_once "./include/zip.php";

if (! $download) {
Exit ();
}
Set_time_limit (60);

$tmpManager = new Tmpmanager (); Let's say we have a class to do the following

$tempfolder = Array ();
$tempfile = Array ();
Let's say we 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);
Handling 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 under different temporary folders

if (file_exists ($curtempdir)) {
$tmpManager->deletedir ($curtempdir); Delete the old folder
}

if (sizeof ($tempfolder) > 0 | | sizeof ($TEMPFILE) > 0) {
mkdir ($curtempdir, 0777); Re-create the folder if you have a folder to package the files
}

if (sizeof ($tempfile) > 0) {
$tmpManager->copyfile ($tempfile, $curtempdir); Copy the file you want to download to the folder you created
}

if (sizeof ($tempfolder) > 0) {
$tmpManager->copyfolder ($tempfolder, $curtempdir); Copy the folder you want to download to the folder you created
}

$zip = new Phpzip ();
$zip-Downloadzip ($curtempdir, "File_". Date (' Ymd '). ". Zip "); Package and download

Transferred from: http://www.cnblogs.com/xiaosuo/

http://www.bkjia.com/PHPjc/364396.html www.bkjia.com true http://www.bkjia.com/PHPjc/364396.html techarticle creating a Zip in PHP is really simple, as long as the following 2 steps can be completed. 1. Please download my prepared zip.php tool class, download and unzip, put the files inside the corresponding directory, I am ...

  • 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.