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