BE CAREFUL!!! The value of $max should actually be 1 less than what it is.
Array(
[0] = 'red'
[1] = 'blue'
[2] = 'black'
[3] = 'green'
[4] = 'orange'
);
Given that array, the [man]count/man function will return 5; however, the array index only goes to 4. That's because count starts at "1" while arrays start their indexing at "0". So, one minor fix to Jazz's code would be to do:
$max = count($files)-1;
And using Installer's code would be perfect for you in that situation:
/**
* Borrowed from Installer
*/
function read_dir($dir, $recurse = false)
{
static $check = true;
if ($check) {
$dir = realpath($dir) . DIRECTORY_SEPARATOR;
if (!is_dir($dir)) {
return false;
}
if (DIRECTORY_SEPARATOR == '/') {
str_replace('\\', '/', $dir);
} else {
str_replace('/', '\\', $dir);
}
$check = false;
}
$dh = dir($dir);
$file_array = array();
while (false !== ($entry = $dh->read())) {
if (($entry != '.') && ($entry != '..')) {
if (is_dir($dir . $entry)) {
$entry .= DIRECTORY_SEPARATOR;
$file_array[$dir]['dirs'][] = $entry;
if ($recurse) {
$file_array = array_merge($file_array, read_dir($dir . $entry, true));
}
} else {
$file_array[$dir]['files'][] = $entry;
}
}
}
$dh->close();
return $file_array;
}
?>
You could use that to do:
<?php
$files = read_dir('.', TRUE);
foreach($list as $directory => $arr1)
{
$max = count($files[$directory]['files'])-1;
$rand[$directory] = $files[$directory]['files'][rand(0, $max)];
echo '<a href="'.$directory.'/index.html"><img src="'$directory.'/images/'.$rand.'" />'.$directory.'</a>';
}
?>
~Brett