Skip to content

Instantly share code, notes, and snippets.

@openam
Created February 26, 2014 17:27
Show Gist options
  • Select an option

  • Save openam/9234264 to your computer and use it in GitHub Desktop.

Select an option

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.
<?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