Showing posts with label zip archive. Show all posts
Showing posts with label zip archive. Show all posts

Wednesday, July 18, 2012

How to add a folder to a zip archive with PHP

If you want to use ZipArchive to archive a folder, you will notice that there is no dedicated method to do this. You will need to make a recursive function to do this.

  function addDirectoryToZip(&$zip, $dir) {
    foreach(glob($dir . '/*') as $file) {
      if(is_dir($file))
        addDirectoryToZip($zip, $file);
      else
        $zip->addFile($file);
    }
  }