Created
February 16, 2018 02:04
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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