php recursive zip with less memory

Solutions on MaxInterview for php recursive zip with less memory by the best coders in the world

showing results for - "php recursive zip with less memory"
Yanis
15 Nov 2020
1$source = <absolute pathname to directory/file to be zipped> . DIRECTORY_SEPARATOR;
2$fileName = "myfiles.zip";
3$destination = <absolute pathname to destination directory> . DIRECTORY_SEPARATOR . $fileName;
4
5function zip_files( $source, $destination ) 
6{
7  $zip = new ZipArchive();
8  if($zip->open($destination, ZIPARCHIVE::CREATE) === true) {
9    $source = realpath($source);
10    if(is_dir($source)) {
11      $iterator = new RecursiveDirectoryIterator($source);
12      $iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
13      $files = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
14      foreach($files as $file) {
15        $file = realpath($file);
16        if(is_dir($file)) {
17          $zip->addEmptyDir(str_replace($source . DIRECTORY_SEPARATOR, '', $file . DIRECTORY_SEPARATOR));
18        }elseif(is_file($file)) {
19          $zip->addFile($file,str_replace($source . DIRECTORY_SEPARATOR, '', $file));
20        }
21      }
22    }elseif(is_file($source)) {
23      $zip->addFile($source,basename($source));
24    }
25  }
26  return $zip->close();
27}
28