Created
February 26, 2014 17:27
-
-
Save openam/9234264 to your computer and use it in GitHub Desktop.
Returns a list of sub-directories given a starting path. Uses DirectoryIterator class.
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 | |
| /** | |
| * get_directories description | |
| * | |
| * @param string $path the path to start from | |
| * @param string $regex that the directories must match, use empty string '' for all | |
| * @return array of directories in the path | |
| */ | |
| function get_directories($path = '.', $regex = '') { | |
| $list = array(); | |
| $file = new DirectoryIterator($path); | |
| while ($file->valid()) { | |
| if($file->isDot()) { | |
| $file->next(); | |
| continue; | |
| } | |
| if($file->isDir()) { | |
| if ($regex) { | |
| if (preg_match($regex, $file->getFilename())) { | |
| $list[] = $file->getFilename(); | |
| } | |
| } else { | |
| $list[] = $file->getFilename(); | |
| } | |
| } | |
| $file->next(); | |
| } | |
| return $list; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment