Created
August 17, 2015 01:01
-
-
Save AlexH-HankIT/bd837f7097d9f1a1e6db to your computer and use it in GitHub Desktop.
Delete a directory and its content
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
| /***** | |
| *@dir - Directory to destroy | |
| *@virtual[optional]- whether a virtual directory | |
| *@link http://webdeveloperplus.com/php/21-really-useful-handy-php-code-snippets/ | |
| */ | |
| function destroyDir($dir, $virtual = false) | |
| { | |
| $ds = DIRECTORY_SEPARATOR; | |
| $dir = $virtual ? realpath($dir) : $dir; | |
| $dir = substr($dir, -1) == $ds ? substr($dir, 0, -1) : $dir; | |
| if (is_dir($dir) && $handle = opendir($dir)) | |
| { | |
| while ($file = readdir($handle)) | |
| { | |
| if ($file == '.' || $file == '..') | |
| { | |
| continue; | |
| } | |
| elseif (is_dir($dir.$ds.$file)) | |
| { | |
| destroyDir($dir.$ds.$file); | |
| } | |
| else | |
| { | |
| unlink($dir.$ds.$file); | |
| } | |
| } | |
| closedir($handle); | |
| rmdir($dir); | |
| return true; | |
| } | |
| else | |
| { | |
| return false; | |
| } | |
| } | |
| - See more at: http://webdeveloperplus.com/php/21-really-useful-handy-php-code-snippets/#sthash.rBXf130A.dpuf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment