James30263 James30263 - 1 month ago 8
CSS Question

How can I find CSS files Recursively?

Is there a way for me to search for css files recursively using the following code?

The directory I'm searching is "themes/Evolution". I want it to search for css files in the sub-directories as well.



<?php

// Does not support flag GLOB_BRACE
function rglob($pattern, $flags = 0) {
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
$files = array_merge($files, rglob($dir.'/'.basename($pattern), $flags));
}
return $files;
}

?>




Answer

Try this:

$directoryWhereToSearch = 'themes/Evolution';
$allFiles = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directoryWhereToSearch));
$cssFiles = new RegexIterator($allFiles, '/\.css$/i');
foreach ($cssFiles as $cssFile) {
    echo $cssFile->getFilename();
}