There are probably about a billion of these things, but I needed one that would manually chmod() if necessary. A little complex of a conditional, but I wanted it condensed.
<?php
/***************************************************
* rm_dir(string $path[, string $delim = "/"]) *
* Deletes the contents of and removes a directory *
* recursively, chmod()'ing if necessary *
* *
* @param string $path Path to the directory *
* @param string $delim Path delimiter ("/") *
* *
* Returns bool *
* true if removal is entirely successful *
* false if removal is not entirely successful *
***************************************************/
function rm_dir($path, $delim = "/")
{
$path .= substr($path, -1) != $delim ? $delim : "";
if(($open = opendir($path)) === false)
return false;
while(($fn = readdir($open)) !== false)
if(($fn != "." && $fn != "..") && ((is_file($path.$fn) && !unlink($path.$fn)) || (is_dir($path.$fn) && !rm_dir($path.$fn))) && (chmod($path.$fn, 0777) && (is_file($path.$fn) && !unlink($path.$fn)) || (is_dir($path.$fn) && !rm_dir($path.$fn))))
return false;
closedir($open);
return rmdir($path);
}
?>
What do you think?