PHP uses zip extension to compress files

Source: Internet
Author: User
Tags zip extension ziparchive

In the company encountered a problem, is the use of Zip packaging users upload files, to provide collective download.

--

The first idea is to use exec to package Linux. But ... exec method, you know, I'm not very willing to use this function.

--

So the internet search, the result is, I underestimated PHP for the operation of Io, to my lesson is, what future problems should be considered how to solve from the code level, if no longer consider the other.

--

How to install the zip extension not much to say, a search on the internet a large.

--

Here it's a simple and practical way to "learn something should try to go to PHP Official document view, where the full demo is given, should not always rely on baidu.com"

The official address for ZIP is also posted here: http://php.net/manual/zh/book.zip.php

--

Here is a brief introduction to zip

Instantiation of

$zip = new \ ziparchive ();

Open method, look at the official interpretation of this method

Mixed Ziparchive::open (string $filename [, int $flags])

The first one, of course, is the path to the zip file you want to open, the second optional parameter

Record several common parameters

      ZIPARCHIVE::CREATE(integer) If it does not exist, create a ZIP archive package.

      ZIPARCHIVE::OVERWRITE(integer) always starts with a new compressed package, which is overwritten if it already exists in this mode.

There are many, not listed here, if necessary, can be found in the document

Here we build my. zip

$res $zip $zip:: CREATE);

Looping through the content I need to package

if ($res = = = TRUE) {    foreach (' Package directory ' as ' directory file ') {        $zip->addfile (' Package file path ', ' name ');    }    $zip->close ();}

This will have a text.zip compression package in your root directory.

This is a simple way to explain the use of packaged files, which will definitely be used later in the process.

The following is the overall code, which may have my business part inside. You can filter it.

        $attachments = those (' attachment ')->whose (' performance ')->is ($performance);        $zip = new \ ziparchive ();        Create a zip-If none is created        $file _path = Data_dir. "/attachment/{$performance->id}/";        $file _name = ' performance Appraisal-'. $performance->name. ' -'. Time (). '. Zip ';        $res = $zip->open ($file _path. $file _name, $zip:: CREATE);        if ($res = = = TRUE) {            foreach ($attachments as $key = = $attachment) {                $zip->addfile ($attachment->path. ' /'. $attachment->name, $attachment->name);            }            $zip->close ();        }

  

PHP uses zip extension to compress files

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.