0

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">');
		}
}
?>

Edited by Rishikeshan: ...

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by mschroeder
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.