Skip to content

Instantly share code, notes, and snippets.

@cognifloyd
Created August 29, 2013 19:21
Show Gist options
  • Save cognifloyd/6382311 to your computer and use it in GitHub Desktop.
Save cognifloyd/6382311 to your computer and use it in GitHub Desktop.
<?
static public function readDirectoryRecursively($path, $suffix = NULL, $returnRealPath = FALSE, $returnDotFiles = FALSE, $followSymLinks = TRUE, /*callable*/ $userFilter = NULL, &$filenames = array()) {
if (!is_dir($path)) {
throw new Exception('"' . $path . '" is no directory.', 1207253462);
}
//by default FilesystemIterator has KEY_AS_PATHNAME|CURRENT_AS_FILEINFO|SKIP_DOTS
$directoryIterator = new \FilesystemIterator($path,$followSymLinks?\FilesystemIterator::FOLLOW_SYMLINKS:NULL);
$suffixLength = strlen($suffix);
$userFilterIsCallable = is_callable($userFilter);
foreach ($directoryIterator as $pathname => $fileInfo) {
/** @var $fileInfo \SplFileInfo */
$filename = $fileInfo->getFilename();
if ($returnDotFiles === FALSE && $filename[0] === '.') {
continue;
}
if ($fileInfo->isFile() && ($suffix === NULL || substr($filename, -$suffixLength) === $suffix)) {
if($userFilterIsCallable && !$userFilter($fileInfo, $pathname, $filename, $returnDotFiles, $suffix)) {
continue;
}
$filenames[] = self::getUnixStylePath(($returnRealPath === TRUE ? realpath($pathname) : $pathname));
}
if ($fileInfo->isDir()) {
self::readDirectoryRecursively($pathname, $suffix, $returnRealPath, $returnDotFiles, $followSymLinks, $userFilter, $filenames);
}
}
return $filenames;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment