June 28, 2015

How to remove a directory which is not empty?

How to remove a directory which is not empty?

function force_rmdir($dir) {
if (is_dir($dir)) {
$paths = scandir($dir);
foreach ($paths as $path) {
if ($path != "." && $path != "..") {
if (filetype($dir."/".$path) == "dir") force_rmdir($dir."/".$path); else unlink($dir."/".$path);
}
}
reset($paths);
rmdir($dir);
}
}

Last updated: June 28, 2015