php zip 只包含新文件 [英] php zip include only new files

查看:29
本文介绍了php zip 只包含新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've run into an issue with the php zip library causing an error 500 if the file is growing to larger than 500MB, probably memory related...

但我尝试对 zip 创建进行 cmd 行,这在我的服务器上运行良好.

But I tried to cmd line the zip creation, which works well on my server.

<?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__);

?>

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

I know how to exclude files and folders, but is there a way to zip only files based on the modified time using this approach?

我用谷歌搜索了几个小时,结果空空如也.

I've googled for hours and came up empty.

为此,这里是创建错误 500 的代码.我的站点大约 1.8GB,它总是在 500MB 处出错~.我应该注意到错误日志是空白的,所以我假设错误的原因是 RAM 限制问题.

for the sake of it, here's the code that was creating the error 500. My site is about 1.8GB, it always errors at 500MB~. I should note the error log is blank, so the cause of the error I'm just assuming to be RAM limit problems.

<?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';            


?>

推荐答案

您可以在 zip 命令中使用 -t 或 -tt 选项,并将修改后的日期存储为变量或直接传入.

You can use the -t or -tt option for the zip command and have your modifed date stored as a variable or just pass one in.

-t 格式为 mmddyyyy 表示起始日期

-t with the format mmddyyyy for from-date

-tt 格式为 mmddyyyy 表示日期前

-tt with the format 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

这篇关于php zip 只包含新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆