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 :

<?php
if (ini_get('display_errors')) {
    ini_set('display_errors', 1);
}
date_default_timezone_set('UTC');
if ($_POST['currentcss']){
	setcookie('currentcss', $_POST['currentcss']);
}
require_once('getcss.php');

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 :

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

Recommended Answers

There are several recursive glob functions out there, for example: http://code.google.com/p/hm2k/source/browse/trunk/code/php/functions/rglob.php

Jump to Post

All 2 Replies

This can be done by combining a few SPL iterators.

<?php

$iterator = new RegexIterator( 
	new RecursiveIteratorIterator( 
		new RecursiveDirectoryIterator('/path/to/your/directory/') 
	), 
	'/.*\.css$/'
);

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.