Forked from assertchris/gist:3bd36713c0a4753ff96b
Last active
August 29, 2015 14:05
-
-
Save dariodiaz/43abad1d4ca65e947461 to your computer and use it in GitHub Desktop.
php: find files in path with extension
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
<?php | |
/** | |
* @param $path | |
* @param $extension | |
* | |
* @return array | |
*/ | |
function getFilesInPathWithExtension($path, $extension) | |
{ | |
$directoryIterator = new RecursiveDirectoryIterator($path); | |
$recursiveIterator = new RecursiveIteratorIterator($directoryIterator); | |
$files = []; | |
foreach ($recursiveIterator as $item) { | |
if ($item->isFile() and $item->getExtension() === $extension) { | |
$files[] = $item; | |
} | |
if ($item->isDir() and $item->getFilename() !== "." and $item->getFilename() !== "..") { | |
$files = array_merge( | |
$files, getFilesInPathWithExtension($item->getPathName(), $extension) | |
); | |
} | |
} | |
return $files; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment