PHP: Search for Files Inside Folder and Sub-Folder

To find all png files inside uploads directory we can use following code.
This would search in all sub-directories as well.

$Directory 	= new RecursiveDirectoryIterator('/wp-content/uploads/');
$Iterator 	= new RecursiveIteratorIterator($Directory);
$Regex 		= new RegexIterator($Iterator, '/^.+\.png$/i', RecursiveRegexIterator::GET_MATCH);	

foreach ($Regex as $file) {
	$file = $file[0];
}

Leave a comment