0

I am currently using the following piece of code to ZIP the current folder:

<?php

$rootPath = realpath('./');

$zip = new ZipArchive();
$zip->open('archive.zip', ZipArchive::CREATE |      ZipArchive::OVERWRITE);

$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);

foreach ($files as $name => $file)

{

if (!$file->isDir())

{

    $filePath = $file->getRealPath();
    $relativePath = substr($filePath, strlen($rootPath) + 1);


    $zip->addFile($filePath, $relativePath);
}

}

$zip->close();

?>

The ZIP file is created fine - however, I noticed that for some reason it fails to include one empty folder that is a subfolder of the current folder. All other files/folders are added to the archive, but the empty folder is completely omitted.

Would anyone be kind enough to point me as to why this is happening?

This is not a duplicate topic - I need to know why ZIP does not add any empty subfolders of the main folder I am zipping. They are empty, but I need them too.

Peter
  • 51
  • 7
  • $zip->addEmptyDir('newDirectory') – Helder De Baere Feb 16 '17 at 15:43
  • Thank you for the response. However, I don't just want to create a new empty directory when creating the ZIP file. Whenever I am zipping a whole folder, it may contain subfolders of subfolders that happen to be empty. How do I do that? – Peter Feb 16 '17 at 15:55
  • Check out this: http://stackoverflow.com/questions/7497733/how-can-use-php-to-check-if-a-directory-is-empty – Helder De Baere Feb 17 '17 at 13:12

0 Answers0