Created
June 27, 2013 21:36
-
-
Save dlueth/5880625 to your computer and use it in GitHub Desktop.
Fetch files matching an optional regex-pattern recursively from any given directory
This file contains 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
getFiles($directory, $recursive = false, $pattern = NULL, $absolute = false) { | |
$return = array(); | |
$iterator = ($recursive === false) ? new \FilesystemIterator($directory, \FilesystemIterator::SKIP_DOTS) : new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory), \RecursiveIteratorIterator::SELF_FIRST); | |
$iterator = ($pattern !== NULL) ? new \RegexIterator($iterator, '/' . $pattern . '/i') : $iterator; | |
$directory = preg_quote($directory . '/', '/'); | |
foreach($iterator as $path) { | |
if($path->isFile() === true) { | |
$return[] = ($absolute !== false) ? $path->getPathname() : preg_replace('/^' . $directory . '/', '', $path->getPathname()); | |
} | |
} | |
sort($return); | |
return $return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment