SHARE
TWEET

Untitled

a guest Aug 18th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   /**
  2.    * Add files and sub-directories in a folder to zip file.
  3.    * @param string $folder
  4.    * @param ZipArchive $zipFile
  5.    * @param int $exclusiveLength Number of text to be exclusived from the file path.
  6.    */
  7.   private function folderToZip($folder, &$zipFile, $exclusiveLength) {
  8.     $handle = opendir($folder);
  9.     while (false !== $f = readdir($handle)) {
  10.       if ($f != '.' && $f != '..') {
  11.         $filePath = "$folder/$f";
  12.         // Remove prefix from file path before add to zip.
  13.         $localPath = substr($filePath, $exclusiveLength);
  14.         if (is_file($filePath)) {
  15.           $zipFile->addFile($filePath, $localPath);
  16.         } elseif (is_dir($filePath)) {
  17.           // Add sub-directory.
  18.           $zipFile->addEmptyDir($localPath);
  19.           $this->folderToZip($filePath, $zipFile, $exclusiveLength);
  20.         }
  21.       }
  22.     }
  23.     closedir($handle);
  24.   }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top