Skip to content

Instantly share code, notes, and snippets.

@TiagoSilvaPereira
Created February 16, 2018 02:04
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 TiagoSilvaPereira/2ea44f8a1f67232b1a4ec844bbdebeb1 to your computer and use it in GitHub Desktop.
Save TiagoSilvaPereira/2ea44f8a1f67232b1a4ec844bbdebeb1 to your computer and use it in GitHub Desktop.
PHP - Removing a folder and all sub folders and files (including hidden files) - this method is better than using glob
<?php
function removeDirectory($dirPath) {
if (! is_dir($dirPath)) {
return false;
}
if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
$dirPath .= '/';
}
if ($handle = opendir($dirPath)) {
while (false !== ($sub = readdir($handle))) {
if ($sub != "." && $sub != ".." && $sub != "Thumb.db") {
$file = $dirPath . $sub;
if (is_dir($file)) {
removeDirectory($file);
} else {
unlink($file);
}
}
}
closedir($handle);
}
rmdir($dirPath);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment