title: Finding empty folders recursively and delete them recursively
body: <p>I have an directory tree which has been passed to array.</p>
  
  <p>I would like to there empty folders inside this array. </p>
  
  <p>How can I determine empty folders like <code>/wp-content/uploads/2014/02/</code> and <code>/wp-content/uploads/2014/</code>.
  How can I delete them recursively.</p>
  
  <p>Here is my array</p>
  
  <pre><code>array (
    0 =&gt; './do-update.php',
    5 =&gt; './wp-config.php',
    6 =&gt; './wp-content/',
    7 =&gt; './wp-content/uploads/',
    8 =&gt; './wp-content/uploads/2013/',
    9 =&gt; './wp-content/uploads/2013/05/',
    10 =&gt; './wp-content/uploads/2013/05/kabeduvarkad-1024x768.jpg',
    26 =&gt; './wp-content/uploads/2013/05/kabeduvarkad2.jpg',
    27 =&gt; './wp-content/uploads/2013/10/',
    28 =&gt; './wp-content/uploads/2014/',
    29 =&gt; './wp-content/uploads/2014/02/',
    30 =&gt; './wp-content/uploads/de.php',
    31 =&gt; './wp-update.tar.gz',
    32 =&gt; './wp-update/',
    33 =&gt; './wp-update/wp-update.tar',
  )
  </code></pre>
  
  <p>Thank you very much to Andresch Serj for him effords.
  Who wants to delete empty folders recursively with performance, you can use this solution.</p>
  
  <pre><code>function list_directory($dir) {
     $file_list = array();
     $stack[] = $dir;
  
     while ($stack) {
          $current_dir = array_pop($stack);
          if ($dh = opendir($current_dir)){
              while (($file = readdir($dh)) !== false) {
                  if ($file !== '.' AND $file !== '..') {
                      $current_file = "{$current_dir}/{$file}";
                      $report = array();
                      if (is_file($current_file)) {
                          $file_list[] = "{$current_dir}/{$file}";
                      } elseif (is_dir($current_file)) {
                          $stack[] = $current_file;
                          $file_list[] = "{$current_dir}/{$file}/";
                      }
                  }
              }
          }
      }
      sort($file_list, SORT_LOCALE_STRING);
      return $file_list;
  }
  
  function remove_emptyfolders($array_filelist){
      $files = array();
      $folders = array();
      foreach($array_filelist as $path){
          // better performance for is_dir function
          if ($path[strlen($path)-1] == '/'){ // check for last character if it is / which is a folder.
              $folders[] = $path;
          }
          else{
              $files[] = $path;
          }
      }
  
      // bos olmayan klasorleri buluyoruz.
      // eger klasor ismi dosya isimlerinin icerisinde gecmiyorsa bos demektir? right?
      $folders_notempty = array();
      foreach($files as $file){
          foreach($folders as $folder){
              if(strpos($file,$folder) !== false){
                  // dublicate olmasin diye key isimlerinin ismine yazdırdık.
                  $folders_notempty[$folder] = $folder;
              }
          }
      }
  
      // bos olmayanla klasorleri, digerlerinden cikariyoruz.
      $folders_empty = array();
      foreach($folders as $folder){
          // eger bos olmayanlarin icerisinde bu dosya yoksa
          if(!in_array($folder, $folders_notempty)){
              $folders_empty[] = $folder;
          }
      }
  
      // once en uzaktan silmeye baslamaliyiz. kisaca tersten.
      $folders_empty = array_reverse($folders_empty);
      $folders_deleted = array();
  
      foreach($folders_empty as $k){
          try{
              $folders_deleted[$k] = 'NOT Succesfull';
              if(rmdir($k)){ $folders_deleted[$k] = 'Deleted'; continue; }
              chmod($k, 0777); 
              if(rmdir($k)){ $folders_deleted[$k] = 'Deleted after chmod'; }
          }catch (Exception $e) {
              print_r($e);
          }
      }
  
      return $folders_deleted;
  
  }
  
  $files = list_directory(getcwd());
  //print_r($files);
  $files_deleted = remove_emptyfolders($files);
  
  print_r($files_deleted);
  </code></pre>