在特定目录中通过PHP创建ZIP

时间:2013-03-11 21:43:00

标签: php

我发现这个很棒的代码可以通过这个链接compress/archive folder using php script

使用PHP创建一个ZIP文件
<?php
// Config Vars 

$sourcefolder = "./"           ; // Default: "./" 
$zipfilename  = "myarchive.zip"; // Default: "myarchive.zip"
$timeout      = 5000           ; // Default: 5000

// instantate an iterator (before creating the zip archive, just
// in case the zip file is created inside the source folder)
// and traverse the directory to get the file list.
$dirlist = new RecursiveDirectoryIterator($sourcefolder);
$filelist = new RecursiveIteratorIterator($dirlist);

// set script timeout value 
ini_set('max_execution_time', $timeout);

// instantate object
$zip = new ZipArchive();

// create and open the archive 
if ($zip->open("$zipfilename", ZipArchive::CREATE) !== TRUE) {
    die ("Could not open archive");
}

// add each file in the file list to the archive
foreach ($filelist as $key=>$value) {
    $zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
}

// close the archive
$zip->close();
echo "Archive ". $zipfilename . " created successfully.";

// And provide download link ?>
<a href="http:<?php echo $zipfilename;?>" target="_blank">
Download <?php echo $zipfilename?></a> 

我想在特定目录中创建zip。

1 个答案:

答案 0 :(得分:3)

$zipfilename  = "/path/to/my/zip/folder/myarchive.zip";

只需确保文件夹为writable即可。