<?php
// Get a list of files
function LIST_DIR ($ dir) {
$result = array();
if (is_dir($dir)){
$ File_dir = scandir ($ dir);
foreach($file_dir as $file){
if ($file == '.' || $file == '..'){
continue;
}
elseif (is_dir($dir.$file)){
$result = array_merge($result, list_dir($dir.$file.'/'));
}
else{
array_push($result, $dir.$file);
}
}
}
return $result;
}
// Get a list
$datalist=list_dir('../');
$ Filename = "./bak.zip"; // finally generated file name (including the path)
if(!file_exists($filename)){
// regenerate files
$ Zip = new ZipArchive (); // use this class, linux need to open zlib, windows to be uncommented before php_zip.dll
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
exit ( 'Unable to open file, or file creation failed');
}
foreach( $datalist as $val){
if(file_exists($val)){
$ Zip-> addFile ($ val, basename ($ val)); // The second parameter is the file name of the package on the compression, if the file may be repeated, you need to take note
}
}
$ Zip-> close (); // close
}
if(!file_exists($filename)){
exit ( "Could not find file"); // create even still may fail. . . .
}
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename='.basename($filename)); //文件名
header ( "Content-Type: application / zip"); // zip format
header ( "Content-Transfer-Encoding: binary"); // tell the browser, which is a binary file
header ( 'Content-Length:'. filesize ($ filename)); // tells the browser, the file size
@readfile($filename);
?>
PHP ZipArchive is bundled with PHP extension classes, can easily compress and decompress ZIP files, make sure first before using PHP ZIP extension is already on, turn on the specific method here is not to say, different platforms open the way PHP amplified online have, if in doubt please share.
Here tidy use php zipArchive be common example of compression and decompression of compressed files for reference.
First, unzip the zip file
$ Zip = new ZipArchive; // create a new object ZipArchive
if($zip->open('test.zip')===TRUE){
$ Zip-> extractTo ( 'images'); // assumed to decompress images in the current folder path
$ Zip-> close (); // close the zip file handling
}
Second, the file compressed into a zip file
$zip=new ZipArchive;
if($zip->open('test.zip',ZipArchive::OVERWRITE)===TRUE){
$ Zip-> addFile ( 'image.txt'); // file name is assumed added image.txt, the current path
$zip->close();
}
Third, file additional content added to the zip file
$zip=new ZipArchive;
$res=$zip->open('test.zip',ZipArchive::CREATE);
if($res===TRUE){
$zip->addFromString('test.txt','file content goes here');
$zip->close();
echo 'ok';
}else{
echo 'failed';
}
Fourth, the folder packaged into a zip file
function addFileToZip($path,$zip){
$ Handler = opendir ($ path); // open the current folder specified by the $ path.
while(($filename=readdir($handler))!==false){
if ($ filename! = "." && $ filename! = "..") {// file folder named '.' and '..' Do not they operate
if (is_dir ($ path. "/". $ filename)) {// If an object is read folders recursively
addFileToZip($path."/".$filename, $zip);
} Else {// the file on the object zip
$zip->addFile($path."/".$filename);
}
}
}
@closedir($path);
}
$zip=new ZipArchive();
if($zip->open('images.zip', ZipArchive::OVERWRITE)=== TRUE){
addFileToZip ( 'images /', $ zip); // call the method, the root directory for the operation to be packaged, and to a method for passing objects ZipArchive
$ Zip-> close (); // close the zip file handling
}