Note that there are some explanatory texts on larger screens.

plurals
  1. POPHPs ZipArchive drops empty directories
    text
    copied!<h1>Problem</h1> <p>I am building an online file manager, for downloading a whole directory structure I am generating a zip file of all subdirectories and files (recursively), therefore I use the <code>RecursiveDirectoryIterator</code>. It all works well, but empty directories are not in the generated zip file, although the dir is handled correctly. This is what i am currently using:</p> <pre><code>&lt;?php $dirlist = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS); $filelist = new RecursiveIteratorIterator($dirlist, RecursiveIteratorIterator::SELF_FIRST); $zip = new ZipArchive(); if ($zip-&gt;open($tmpName, ZipArchive::CREATE) !== TRUE) { die(); } foreach ($filelist as $key=&gt;$value) { $result = false; if (is_dir($key)) { $result = $zip-&gt;addEmptyDir($key); //this message is correctly generated! DeWorx_Logger::debug('added dir '.$key .'('.$this-&gt;clearRelativePath($key).')'); } else { $result = $zip-&gt;addFile($key, $key); } } $zip-&gt;close(); </code></pre> <p>If I ommit the <code>FilesystemIterator::SKIP_DOTS</code> I end up having a <code>.</code> <strong>file</strong> in all directories.</p> <h1>Conclusion</h1> <p>The iterator works, the <code>addEmptyDir</code> call gets executed (the result is checked too!) correctly, creating a zip file with various zip tools works with empty directories as intendet. Is this a bug in phps <code>ZipArchive</code> (<a href="http://www.php.net/manual/en/class.ziparchive.php" rel="nofollow">php.net</a> lib or am I missing something? I don't want to end up creating dummy files just to keep the directory structure intact.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload