0

我是 SO 新手,也是 PHP 新手。我在网上找到了一个压缩目录的脚本,我已经对其进行了编辑,以便它将压缩文件发送到浏览器进行下载,然后从服务器中删除该文件。

它工作正常,但是我想压缩多个目录而不是一个。

我需要如何改变我的脚本来完成这个?

$date = date('Y-m-d');

$dirToBackup = "content";
$dest = "backups/"; // make sure this directory exists!
$filename = "backup-$date.zip";

$archive = $dest.$filename;


function folderToZip($folder, &$zipFile, $subfolder = null) {
    if ($zipFile == null) {
        // no resource given, exit
        return false;
    }
    // we check if $folder has a slash at its end, if not, we append one
    $folder .= end(str_split($folder)) == "/" ? "" : "/";
    $subfolder .= end(str_split($subfolder)) == "/" ? "" : "/";
    // we start by going through all files in $folder
    $handle = opendir($folder);
    while ($f = readdir($handle)) {
        if ($f != "." && $f != "..") {
            if (is_file($folder . $f)) {
                // if we find a file, store it
                // if we have a subfolder, store it there
                if ($subfolder != null)
                    $zipFile->addFile($folder . $f, $subfolder . $f);
                else
                    $zipFile->addFile($folder . $f);
            } elseif (is_dir($folder . $f)) {
                // if we find a folder, create a folder in the zip 
                $zipFile->addEmptyDir($f);
                // and call the function again
                folderToZip($folder . $f, $zipFile, $f);
            }
        }
    }
}

// create the zip
$z = new ZipArchive();
$z->open($archive, ZIPARCHIVE::CREATE);
folderToZip($dirToBackup, $z);
$z->close();

// download the zip file
$file_name = basename($archive);

header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=$file_name");
header("Content-Length: " . filesize($archive));

readfile($archive);

// delete the file from the server
unlink($archive);
exit;

谢谢你的帮助!

厄玛

4

1 回答 1

0

将 $dirToBackup 设置为

$dirToBackup = array("restricted","ci");

接着 :

foreach($dirToBackup as $d){
    folderToZip($d, $z, $d);
}

就这样。问候,

于 2013-03-04T13:55:50.447 回答