<?php
/**
* Delete a file, or a folder and its contents
*
* @author Aidan Lister <aidan@php.net>
* @version 1.0.2
* @param string $dirname Directory to delete
* @return bool Returns TRUE on success, FALSE on failure
*/
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);
}
?>
http://aidan.dotgeek.org/lib/?file=function.rmdirr.php
shasha said,
November 27, 2006 @ 10:41 pm
I try it and it works…. thanks
shasha
rizal said,
December 17, 2006 @ 2:25 am
at first i thought it will only delete files in folder specified. But it delete all files and folder AND ALSO FOLDER YOU USE FOR THE FUNCTION!
@@”
But this is what i need. Although a bit dangerous..
To play safe i use like this:
if (file_exists($dir_folder_to_delete)) {
rmdirr($dir_folder_to_delete);
}
Putera Emas said,
December 20, 2006 @ 7:51 pm
oppp.. sorry to warn you. it will completely delete the contents in the folder.
kece said,
May 3, 2007 @ 9:40 pm
powerful stuph. surprisingly fast too
Gede said,
July 14, 2008 @ 8:47 pm
Thanks bro.. totally helpful
www.fimizone.org said,
November 19, 2008 @ 5:52 pm
cool.
shivkumar yadav said,
March 10, 2009 @ 6:03 pm
i want to know is there any function to check folder is empy or not
Bart Huisman said,
April 21, 2009 @ 6:16 pm
i use it this way:
function rmdirr($dirname){
// Sanity check
$dirname = “../albums/” . $dirname;
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 ”;
}
else{
echo something went wrong.’;
}
changes i made: other folder, could be removed if you want.
also the quotes are wrong when i copy it from here, it has to be straight quotes, no “diagonal” quotes (sorry, dont know a better english explenation)
also the loop when checking if there ale files in the flder, to delete the files, i changed it in unlink, becouse with the original he did not deleted the files, only give a php error the folder is nog empty….
Bart Huisman said,
April 21, 2009 @ 6:19 pm
hmm, i see now my quotes are wrong now too, so you have to check if the quotes (single and double) are the good ones for php, not the “diagonal” ones.
ok, just a test to see if you could see the difrence between the quotes:
” , ‘ , ` , ” , ‘ , ’
Bart Huisman said,
April 21, 2009 @ 6:22 pm
ok, theres used an bad script to avoid hacking i think, all straight qoutes are changed is diagonal ones, and not set back when showing here, so you just have to chenge the quotes by yourself….
Trd said,
May 6, 2009 @ 4:35 pm
Very helpful. Thanks a lot!!
paradox said,
July 3, 2009 @ 2:31 am
@ Bart Huisman; Very nice. Where you have this script hosted? You make it very easy for me to deface your entire website.
Fero said,
September 17, 2009 @ 2:29 pm
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.