Guest User

Untitled

a guest
Jan 23rd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. $dir = 'pptx/test/compiled';
  2. $zip_file = 'pptx/test/file.zip';
  3.  
  4. // Get real path for our folder
  5. $rootPath = realpath($dir);
  6.  
  7. // Initialize archive object
  8. $zip = new ZipArchive();
  9. $zip->open($zip_file, ZipArchive::CREATE | ZipArchive::OVERWRITE);
  10.  
  11. // Create recursive directory iterator
  12. /** @var SplFileInfo[] $files */
  13. $files = new RecursiveIteratorIterator(
  14. new RecursiveDirectoryIterator($rootPath),
  15. RecursiveIteratorIterator::LEAVES_ONLY
  16. );
  17.  
  18. foreach ($files as $name => $file)
  19. {
  20. // Skip directories (they would be added automatically)
  21. if (!$file->isDir())
  22. {
  23. // Get real and relative path for current file
  24. $filePath = $file->getRealPath();
  25. $relativePath = substr($filePath, strlen($rootPath) + 1);
  26.  
  27. // Add current file to archive
  28. $zip->addFile($filePath, $relativePath);
  29. }
  30. }
  31.  
  32. // Zip archive will be created only after closing object
  33. $zip->close();
Add Comment
Please, Sign In to add comment