Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement