我正在使用这个PHP类:http://www.phpclasses.org/browse/file/9524.html
我使用此代码使其工作:
include('scripts/zip.php');
$directoryToZip = "./"; // This will zip all the file(s) in this present working directory
$outputDir = 'backup/'; //Replace "/" with the name of the desired output directory.
$zipName = 'backup_'.date('Y-m-d').'1.zip';
// If backup already exists, kill it
if(file_exists($outputDir.$zipName)){
unlink($outputDir.$zipName);
}
$createZipFile = new CreateZipFile;
/*
// Code to Zip a single file
$createZipFile->addDirectory($outputDir);
$fileContents=file_get_contents($fileToZip);
$createZipFile->addFile($fileContents, $outputDir.$fileToZip);
*/
//Code toZip a directory and all its files/subdirectories
$createZipFile->zipDirectory($directoryToZip,$outputDir);
$fd = fopen($outputDir.$zipName, "wb");
fwrite($fd,$createZipFile->getZippedfile());
fclose($fd);
现在你看到我告诉它.zip使用这个变量的所有目录和文件:
$directoryToZip = "./";
我需要做一个例外: 我不希望脚本.zip备份目录。
如何添加例外?
答案 0 :(得分:1)
您应该覆盖“parseDirectory”方法,如下所示:
<?php
include('zip.php');
class myCreateZipFile extends CreateZipFile {
protected function parseDirectory($rootPath, $separator="/"){
global $directoryToZip, $outputDir;
$fileArray1 = parent::parseDirectory($rootPath, $separator);
$prefix = $directoryToZip.$separator.$outputDir;
$fileArray2 = array();
foreach ($fileArray1 as $file) {
if (strncmp($file, $prefix, strlen($prefix)) != 0) {
$fileArray2[] = $file;
}
}
return($fileArray2);
}
}
$directoryToZip = "./";
$outputDir = 'backup/';
$zipName = 'backup_'.date('Y-m-d').'1.zip';
@unlink($outputDir.$zipName);
$createZipFile = new myCreateZipFile;
$createZipFile->zipDirectory($directoryToZip, $outputDir);
if ($fd = fopen($outputDir.$zipName, "wb")) {
fwrite($fd,$createZipFile->getZippedfile());
fclose($fd);
}
?>