Skip to content

Instantly share code, notes, and snippets.

@dariodiaz
Forked from assertchris/gist:3bd36713c0a4753ff96b
Last active August 29, 2015 14:05
Show Gist options
  • Save dariodiaz/43abad1d4ca65e947461 to your computer and use it in GitHub Desktop.
Save dariodiaz/43abad1d4ca65e947461 to your computer and use it in GitHub Desktop.
php: find files in path with extension
<?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