Is there any way to list *.css from directories/subdirectories recursively? I want to make a css selector... The following script lists nonrecursively... But i need a recursive list...

themeselect.php :

if (ini_get('display_errors')) {
    ini_set('display_errors', 1);
if ($_POST['currentcss']){
	setcookie('currentcss', $_POST['currentcss']);

function right($value, $count){
    return substr($value, ($count*-1));

function left($string, $count){
    return substr($string, 0, $count);

echo '<select name="currentcss" id="currentcss">';
foreach (glob(getcwd() . '/*.css') as $filename) {
    $filename = right($filename, (strlen($filename) - strlen(getcwd()) - 1));
    echo '<option value="' . $filename . '">' . $filename . '</option>';
echo '</select>';

getcss.php :

function getcss(){
	if (file_exists($_COOKIE["currentcss"])) {
		echo ('<LINK href="' . $_COOKIE["currentcss"] . '" rel="stylesheet" type="text/css">');
		echo ('<LINK href="default.css" rel="stylesheet" type="text/css">');

Recommended Answers

There are several recursive glob functions out there, for example:

Jump to Post

All 2 Replies

This can be done by combining a few SPL iterators.


$iterator = new RegexIterator( 
	new RecursiveIteratorIterator( 
		new RecursiveDirectoryIterator('/path/to/your/directory/') 

foreach( $iterator  as $file ){
	echo $file.PHP_EOL;

Where $file in the foreach loop will be an instance of SplFileInfo.

You could also skip the regex iterator and create your own filter iterator that extends FilterIterator but you'd have to write your own accept() method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.