Skip to content

Instantly share code, notes, and snippets.

@javierav
Created April 7, 2012 16:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save javierav/2330131 to your computer and use it in GitHub Desktop.
Save javierav/2330131 to your computer and use it in GitHub Desktop.
Script PHP para borrar de forma recursiva un directorio
<?php
/*
* this function removes a directory and its contents.
* use with careful, no undo!
*/
function rmdir_recursive($dir) {
$files = scandir($dir);
array_shift($files); // remove '.' from array
array_shift($files); // remove '..' from array
foreach ($files as $file) {
$file = $dir . '/' . $file;
if (is_dir($file)) {
rmdir_recursive($file);
rmdir($file);
} else {
unlink($file);
}
}
rmdir($dir);
}
// remove directory /home/nash/tmp
$dir = '/home/nash/tmp';
rmdir_recursive($dir);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment