tags:

views:

171

answers:

3
<?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.

+1  A: 

you have to make sure your webserver is able to delete those files. check the permissions.

RageZ
i am working in my local machine. Kindly advice me How to check your Criteria?
Fero
your would have to check the security tab of the property of concerned folders
RageZ
+3  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...

kkyy
Keep in mind that the `$dirname = "TEST/";` line at the top prevents the function from being used recursively.
too much php
Good point :) Note to self: remember to read what you have copy-pasted...
kkyy
i tried your code its not working as per my specifications. The following error is occurring when i used it. Fatal error: Call to undefined function rmdirr() in E:\Xampp\xampp\htdocs\delete_FILE\delete_FILE.php on line 26
Fero
awaiting for ur help
Fero
Works for me. Please double-check that you have named the function correctly...
kkyy
A: 

You are deleting the files in 1 level only. Your code tries to delete the folder TEST//New Folder using unlink instead of rmdir. You have to check if it's a folder or not, then rmdir or unlink it.

Aziz
could you kindly guide me how to do that...
Fero
check kkyy's answer .. he fixed it :)
Aziz
i tried kkyy's code its not working as per my specifications. The following error is occurring when i used it.Fatal error: Call to undefined function rmdirr() in E:\Xampp\xampp\htdocs\delete_FILE\delete_FILE.php on line 26
Fero
make sure the function name is `rmdirr`. If you changed that, you need to change it also in the line where it says `//Recurse`.
Aziz