Excluindo Conteúdo do Diretório e Conteúdo do SubDirectory

Eu configurei algum PHP para deletar um diretório, seu conteúdo, qualquer subdiretório e seu conteúdo ... Eu sou novo em PHP, então eu definitivamente estou fazendo algo ERRADO ou estou fazendo algo da maneira mais ineficiente.

Procurando por algumas referências ou sugestões sobre como fazer isso melhor ...

By the way, esse código funciona bem. Usando o PHP 5.3.8.

chmod($main_dir, 0755);
if ($handle = opendir($main_dir)) {
    while (false !== ($entry = readdir($handle))) { 
        $absolute_path = $main_dir.'/'.$entry;
        if ($entry != "." && $entry != "..") {      
            chmod($absolute_path, 0755);
            unlink($absolute_path);

            //check if any folders exist, then delete files within
            if (file_exists($absolute_path) && is_dir($absolute_path)) {
                if ($child_handle = opendir($absolute_path)) {
                    while (false !== ($child_entry = readdir($child_handle))) {             
                    $child_absolute_path = $absolute_path.'/'.$child_entry;
                        if ($child_entry != "." && $child_entry != "..") {              
                            chmod($child_absolute_path, 0755);
                            unlink($child_absolute_path);
                        }
                    }
                    closedir($child_handle);
                }
            }
            rmdir($absolute_path);
        }
    }
    closedir($handle);
}
rmdir($main_dir);

Alguma ideia? Muito apreciado! Estou usando PHP 5.3.8

questionAnswers(1)

yourAnswerToTheQuestion