Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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...

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.

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


?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
177 views
Welcome To Ask or Share your Answers For Others

1 Answer

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 with the format mmddyyyy for from-date

-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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...