title: PHP Recursively delete empty directories with SPL iterators
body: <p>I'm struggling with how to delete a tree of empty directories in PHP using SPL iterators. Consider the following directory structure in which all directories are empty:</p>
  
  <blockquote>
    <p>/topdir</p>
  
  <pre><code>  level1
  
         level2
  </code></pre>
  </blockquote>
  
  <p>I've tried the following:</p>
  
  <pre><code>$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(
      '/topdir', RecursiveIteratorIterator::CHILD_FIRST
  ));
  
  foreach ($it as $file) {
      if ($file-&gt;isDir()) {
          rmdir((string)$file);
      }
  }
  </code></pre>
  
  <p>But <code>RecursiveIteratorIterator::CHILD_FIRST</code> prevents the bottom level file from being part of the loop and I get the standard <code>Directory not empty</code> E_WARNING because level1 is not empty.</p>
  
  <p>How do I recursively delete a tree of empty directories using SPL Iterators? Note: I know how to do this with <code>glob</code>, <code>scandir</code>, etc. Please do not offer these/similar functions as solutions.</p>
  
  <p>I feel like I must be missing something very elementary here ...</p>