rmdir — Remove um diretório no PHP
<?php
function deleteDirectory($dirname,$only_empty=false) {
if (!is_dir($dirname))
return false;
$dscan = array(realpath($dirname));
$darr = array();
while (!empty($dscan)) {
$dcur = array_pop($dscan);
$darr[] = $dcur;
if ($d=opendir($dcur)) {
while ($f=readdir($d)) {
if ($f=='.' || $f=='..')
continue;
$f=$dcur.'/'.$f;
if (is_dir($f))
$dscan[] = $f;
else
unlink($f);
}
closedir($d);
}
}
$i_until = ($only_empty)? 1 : 0;
for ($i=count($darr)-1; $i>=$i_until; $i--) {
echo "\nDeleting '".$darr[$i]."' ... ";
if (rmdir($darr[$i]))
echo "ok";
else
echo "FAIL";
}
return (($only_empty)? (count(scandir)<=2) : (!is_dir($dirname)));
}
?>
//Outra
<?php
function rmdirtree($dirname) {
if (is_dir($dirname)) { //Operate on dirs only
$result=array();
if (substr($dirname,-1)!='/') {$dirname.='/';} //Append slash if necessary
$handle = opendir($dirname);
while (false !== ($file = readdir($handle))) {
if ($file!='.' && $file!= '..') { //Ignore . and ..
$path = $dirname.$file;
if (is_dir($path)) { //Recurse if subdir, Delete if file
$result=array_merge($result,rmdirtree($path));
}else{
unlink($path);
$result[].=$path;
}
}
}
closedir($handle);
rmdir($dirname); //Remove dir
$result[].=$dirname;
return $result; //Return array of deleted items
}else{
return false; //Return false if attempting to operate on a file
}
}
?>