Skip to content

Instantly share code, notes, and snippets.

@bablukpik
Forked from irazasyed/gist:4340722
Last active November 15, 2018 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bablukpik/68a10f55a6ba87433685d9f1ab17d5de to your computer and use it in GitHub Desktop.
Save bablukpik/68a10f55a6ba87433685d9f1ab17d5de 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);
}
}
}
//OR
To delete all files in a folder:
array_map('unlink', glob("$dirname/*.*"));
And then we can remove the folder:
rmdir($dirname);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment