Q:

how to delete a folder which is empty using PHP

<?php 
function rmdirr($dirname){
    // Sanity check
    $dirname = "TEST/";
    if (!file_exists($dirname)) {
        return false;
    }

    // Simple delete for a file
    if (is_file($dirname)) {
        return unlink($dirname);
    }

    // Loop through the folder
    $dir = dir($dirname);
    while (false !== $entry = $dir->read()) {
        // Skip pointers
        if ($entry == "." || $entry == "..") {
            continue;
        }

        // Recurse
        unlink("$dirname/$entry");
    }

    // Clean up
    $dir->close();
    return rmdir($dirname);
}
if (rmdirr($_GET['map'])){
    echo "TEST FERo";
}
else{
    echo "something went wrong.";
}
?>

Its working well and good. But if i need to delete the folder which contains some files and empty folder. In this case it delete all the files but not the empty folder. It throws an exception like…
Warning: unlink(TEST//New Folder) [function.unlink]: Permission denied 
  in E:\Xampp\xampp\htdocs\delete_FILE\delete_FILE.php on line 23

Warning: rmdir(TEST/) [function.rmdir]: Directory not empty in
  E:\Xampp\xampp\htdocs\delete_FILE\delete_FILE.php on line 28

What is the possible way to delete even the folder is empty.

A:

Just use your rmdirr function recursively in the while loop instead of unlink;
function rmdirr($dirname){ 
  // Sanity check 
  if (!file_exists($dirname)) { return false; }

  // Simple delete for a file 
  if (is_file($dirname)) { return unlink($dirname); }

  // Loop through the folder 
  $dir = dir($dirname); 
  while (false !== $entry = $dir->read()) { 
    // Skip pointers 
    if ($entry == "." || $entry == "..") { continue; }

    // Recurse
    rmdirr("$dirname/$entry"); 

  }

  // Clean up 
  $dir->close(); 
  return rmdir($dirname); 
} 

This way it'll also take care of non-empty subfolders...

