2015/03/15

How to remove directory with all its child subdirectories and files in PHP ?

function delete_dir($dirPath) {
$dir = opendir($dirPath);
while(($file = readdir($dir)) !== false) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($dirPath . '/' . $file) ) {
delete_dir($dirPath . '/' . $file);
}
else {
unlink($dirPath . '/' . $file);
}
}
}
rmdir($dirPath);
closedir($dir);
}
view raw gistfile1.php hosted with ❤ by GitHub