Skip to content

Instantly share code, notes, and snippets.

@irazasyed
Created December 19, 2012 21:32
Show Gist options
  • Save irazasyed/4340722 to your computer and use it in GitHub Desktop.
Save irazasyed/4340722 to your computer and use it in GitHub Desktop.
PHP: Remove directory and its contents
/**
* Remove the directory and its content (all files and subdirectories).
* @param string $dir the directory name
*/
function rmrf($dir) {
foreach (glob($dir) as $file) {
if (is_dir($file)) {
rmrf("$file/*");
rmdir($file);
} else {
unlink($file);
}
}
}
@ayouzi
Copy link

ayouzi commented Aug 26, 2020

if error not found method rmrf
use:
if (is_dir($file)) {
$this->rmrf("$file/*");
rmdir($file);
}

or if use this method in another methos static
use:
if (is_dir($file)) {
self::rmrf("$file/*");
rmdir($file);
}

💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment