Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $dir = 'pptx/test/compiled';
- $zip_file = 'pptx/test/file.zip';
- // Get real path for our folder
- $rootPath = realpath($dir);
- // Initialize archive object
- $zip = new ZipArchive();
- $zip->open($zip_file, ZipArchive::CREATE | ZipArchive::OVERWRITE);
- // Create recursive directory iterator
- /** @var SplFileInfo[] $files */
- $files = new RecursiveIteratorIterator(
- new RecursiveDirectoryIterator($rootPath),
- RecursiveIteratorIterator::LEAVES_ONLY
- );
- foreach ($files as $name => $file)
- {
- // Skip directories (they would be added automatically)
- if (!$file->isDir())
- {
- // Get real and relative path for current file
- $filePath = $file->getRealPath();
- $relativePath = substr($filePath, strlen($rootPath) + 1);
- // Add current file to archive
- $zip->addFile($filePath, $relativePath);
- }
- }
- // Zip archive will be created only after closing object
- $zip->close();
Add Comment
Please, Sign In to add comment