php zip仅包含新文件

时间:2013-08-16 17:07:29

标签: php zip

我遇到了php zip库的问题,如果文件增长到500MB以上,可能导致错误500,可能与内存有关......

但是我尝试使用cmd来创建zip,这在我的服务器上运行良好。

<?php
set_time_limit(10000);
// Make Zip name
$zipname = "main_backup.zip";

// Make a zip file
$cmd = `zip -r $zipname * -x filelist.php -x $zipname`;
if($cmd){
    echo 'zip created';
}
else{
    echo 'failed';  
}
unlink(__FILE__);

?>

我知道如何排除文件和文件夹,但是有没有办法使用这种方法根据修改的时间仅压缩文件?

我用谷歌搜索了几个小时,然后空了。

为此,这里是创建错误500的代码。我的网站大约是1.8GB,总是500MB的错误。我应该注意错误日志是空白的,所以错误的原因我只是假设是RAM限制问题。

<?php
    $zip = new ZipArchive;
    $zip_name = "test.zip";
    $res = $zip->open($zip_name, ZipArchive::CREATE);

/**
 * Real Recursive Directory Iterator
 */
class RRDI extends RecursiveIteratorIterator {
  /**
   * Creates Real Recursive Directory Iterator
   * @param string $path
   * @param int $flags
   * @return DirectoryIterator
   */
  public function __construct($path, $flags = 0) {
    parent::__construct(new RecursiveDirectoryIterator($path, $flags));
  }
}

/**
 * Real RecursiveDirectoryIterator Filtered Class
 * Returns only those items which filenames match given regex
 */
class AdvancedDirectoryIterator extends FilterIterator {
  /**
   * Regex storage
   * @var string
   */
  private $regex;
  /**
   * Creates new AdvancedDirectoryIterator
   * @param string $path, prefix with '-R ' for recursive, postfix with /[wildcards] for matching
   * @param int $flags
   * @return DirectoryIterator
   */
  public function  __construct($path, $flags = 0) {
    if (strpos($path, '-R ') === 0) { $recursive = true; $path = substr($path, 3); }
    if (preg_match('~/?([^/]*\*[^/]*)$~', $path, $matches)) { // matched wildcards in filename
      $path = substr($path, 0, -strlen($matches[1]) - 1); // strip wildcards part from path
      $this->regex = '~^' . str_replace('*', '.*', str_replace('.', '\.', $matches[1])) . '$~'; // convert wildcards to regex 
      if (!$path) $path = '.'; // if no path given, we assume CWD
    }
    parent::__construct($recursive ? new RRDI($path, $flags) : new DirectoryIterator($path));
  }
  /**
   * Checks for regex in current filename, or matches all if no regex specified
   * @return bool
   */
  public function accept() { // FilterIterator method
    return $this->regex === null ? true : preg_match($this->regex, $this->getInnerIterator()->getFilename());
  }
}

foreach (new AdvancedDirectoryIterator('-R *') as $i){
    //$fullpath = str_replace(',','',$i->getPath()).'/'.$i->getFilename();
    //echo $fullpath.'<br />';
    if ($i->isFile() && $i->getFilename()!='filelist.php') {
        //echo $i->getFilename() . " " . $i->getMTime() . "<br />";
        if($i->getMTime()>='0'){
            $array[] = substr($i->getPathname(), 2);                    
        }

    }   
};
// will output all php files in CWD and all subdirectories


        foreach($array as $files) {
            $zip_array[] = files;
            $zip->addFile($files);          
        }       

        $zip->close();
        echo 'done';            


?>

1 个答案:

答案 0 :(得分:0)

您可以对zip命令使用-t或-tt选项,并将修改后的日期存储为变量或只传入一个。

-t,格式为mmddyyyy,用于发布日期

-tt,格式为mmddyyyy for before-date

//Zips up all files in current directory that were dated 08152013 or later

zip -r -t 08152013 $zipname * -x filelist.php -x $zipname
相关问题