I am trying to get this php code to search ALL the directories on my site for images. I then want is to store the image names and directories in an array and display one random picture.
My problem is that it seems to erase the contents of the array everytime it searches a new directory, and is completely empty by the time is tries to run the random function. I would appreciate any tips on how I can solve this problem.
Thanks
<?php
$basedir = "/home/vsites/velcron/public_html/php/archive/";
$result = $dirfiles = $dirnames = array();
$target="_blank";
function listall($dir)
{
// temp arrays
$dir_files = $dir_subdirs = $dir_names = array();
chdir($dir);
$handle = @opendir($dir) or die( "Directory \"$dir\"not found.");
// loop through all directory entries
while($entry = readdir($handle))
{
if(is_dir($entry) && $entry != ".." && $entry != ".")
{
$dir_subdirs[] = $entry;
}
elseif($entry != ".." && $entry != ".")
{
if (ereg(".gif",$entry) || ereg(".jpg",$entry) || ereg(".JPG",$entry) || ereg(".GIF",$entry))
{
$dir_names[] = $dir;
$dir_files[] = $entry;
$foo = substr("$dir", 33);
$a ="http://www.buttonyourpants.com/". $foo ."". $entry;
$result = array_merge_recursive ($result, $a);
}
}
}
// sub directories
for($i=0; $i<count($dir_subdirs); $i++)
{
listall( "$dir$dir_subdirs[$i]/");
}
// displays all images
// for($i=0; $i<count($result); $i++)
// {
// echo "<br><img src=\"$result[$i]\">";
// }
closedir($handle);
}
// display random image??
function randompic($result)
{
$random_no= count($result);
$random=$random_no-1;
mt_srand ((double) microtime () * 1000000);
$rnd= mt_rand(0,$random);
echo "<br><img src=\"$result[$rnd]\">";
}
listall($basedir);
randompic($result);
?>