Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP压缩类。压缩站点根目录中除一个以外的所有目录_Php_Class_Function - Fatal编程技术网

PHP压缩类。压缩站点根目录中除一个以外的所有目录

PHP压缩类。压缩站点根目录中除一个以外的所有目录,php,class,function,Php,Class,Function,我正在使用这个PHP类: 我使用此代码使其工作: 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 = 'backu

我正在使用这个PHP类:

我使用此代码使其工作:

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 = "./";
我需要做一个例外: 我不想让脚本压缩备份目录

如何添加异常?

您应该覆盖“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); 
}

?>


无法访问代码,能否将其发布到pastebin?我忘了提到我已经解决了这个问题。但我会接受你的答案,假设它有效:)