I have a large directory that contains 100s of additional folders, each of which contains yet more subdirectories. It's three levels deep - and too complicated for me to hack through, but here's what I want to do:
I would like to display a random image from a random subdirectory within a random folder within one large directory. My code is supposed to select an image from the deepest level first, then work out from there if subdirectories do not exist. When finished, it should the ultimate random image generator - completely random!
Oh yeah, I also want to avoid "thumbs.db" files...
Here's what I have so far:
(please help rewrite if you can)
<?php
//BEGIN CODE ONE...
$maindirectory = 'BigFolder' ;
$md = opendir($maindirectory);
// The following loop scans the directory specified ignoring folders and Thumbs.db
while (false !== ($foldername = readdir($md))) {
if($foldername == "Thumbs.db") {
}elseif(is_dir($foldername)){
//BEGIN CODE TWO...
$subdirectory = $maindirectory.'/'.$folder[$pickfolder] ;
$sd = opendir($subdirectory);
// The following loop scans the directory specified ignoring folders and Thumbs.db
while (false !== ($subfoldername = readdir($sd))) {
if($subfoldername == "Thumbs.db") {
}elseif(is_dir($subfoldername)){
//BEGIN CODE THREE...
$dblsubdirectory = $maindirectory.'/'.$folder[$pickfolder].'/'.$subfolder[$picksubfolder] ;
$dblsd = opendir($dblsubdirectory);
// The following loop scans the directory specified ignoring folders and Thumbs.db
while (false !== ($dblsubfoldername = readdir($dblsd))) {
if($dblsubfoldername == "Thumbs.db" || is_dir($dblsubfoldername)){
}else{
$dblsubfolder[] = $dblsubfoldername;
}
}
// Generate a random number
$dblsubfoldernum = count($dblsubfolder);
$realdblsubfoldernum = ($dblsubfoldernum-1);
srand((double)microtime()*1000000);
$pickdblsubfolder = rand(0,$realdblsubfoldernum);
// print the result
echo "<DIV align='center'><IMG SRC='$dblsubdirectory/$dblsubfolder[$pickdblsubfolder]' ALT='$dblsubfolder[$pickdblsubfolder]' BORDER='0'></DIV>";
// ... END CODE THREE
}else{
$subfolder[] = $subfoldername;
}
}
// Generate a random number
$subfoldernum = count($subfolder);
$realsubfoldernum = ($subfoldernum-1);
srand((double)microtime()*1000000);
$picksubfolder = rand(0,$realsubfoldernum);
// print the result
echo "<DIV align='center'><IMG SRC='$subdirectory/$subfolder[$picksubfolder]' ALT='$subfolder[$picksubfolder]' BORDER='0'></DIV>";
// ... END CODE TWO
}else{
$folder[] = $foldername;
}
}
// Generate a random number
$foldernum = count($folder);
$realfoldernum = ($foldernum-1);
srand((double)microtime()*1000000);
$pickfolder = rand(0,$realfoldernum);
// print the result
echo "<DIV align='center'><IMG SRC='$maindirectory/$folder[$pickfolder]' ALT='$folder[$pickfolder]' BORDER='0'></DIV>";
// ... END CODE ONE
?>