getDirArray(Path,Filter,Sorted): Returns an array of the files in a directory,
filtered by regular expression and either sorted or randomized. Good for
random pictures and graphics.
<?php
// Returns an array of files from a given directory
// Parameters:
// $Path: directory to get files for (trailing '/' optional)
// $Filter: case-insensitive regular expression (e.g.. ".*html?")
// $Sorted: any value other than "True"/"true" will randomize the array
// Returns 0 if no files match criteria or an array containing the results
// Does not return directories (including '.' and '..')
// adapted from www.php.net documentation
// Very useful for random image display on a page without having to
// use some arbitrary naming scheme
function getDirArray($Path="./",$Filter=".*",$Sorted="True")
{
$handle=opendir($Path);
while ($file = readdir($handle)) {
if (is_file("$Path/$file") && eregi("$Filter", $file)) {
$dirArray[] = $file;
}
}
closedir($handle);
if (!is_array($dirArray)) {
return 0;
}
if (strcasecmp($Sorted,"True")) {
shuffle($dirArray); // May not work on some systems. See php docs
} else {
sort($dirArray);
}
return $dirArray;
}
// Check these samples to see how it works in practice