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

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.