Last active
December 16, 2015 11:19
-
-
Save jesse1981/5426394 to your computer and use it in GitHub Desktop.
PHP function to return a directory tree to an array.
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
| function directoryToArray($directory, $recursive) { | |
| $array_items = array(); | |
| if ($handle = opendir($directory)) { | |
| while (false !== ($file = readdir($handle))) { | |
| if (($file != "." && $file != "..") && | |
| (is_dir($directory. "/" . $file)) && | |
| ($recursive)) $array_items = array_merge($array_items, | |
| directoryToArray($directory. "/" . $file, $recursive)); | |
| if (is_dir($directory. "/" . $file)) { | |
| $file = $directory . "/" . $file; | |
| $array_items[] = preg_replace("/\/\//si", "/", $file); | |
| } | |
| else { | |
| $file = $directory . "/" . $file; | |
| $array_items[] = preg_replace("/\/\//si", "/", $file); | |
| } | |
| } | |
| closedir($handle); | |
| } | |
| return $array_items; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment