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
?>

    im confused.

    you want random, yet you want it to try and pull images from the deepest directory first? that not be very random, in fact it will ignore all files except those in the deepest directorys.

      Once my code chooses a random "Folder" from the BIG DIRECTORY, it will search for sudirectories, pick one at random, then display a random image file. If subdirectories do not exist, then my code will display a random image files from the random "Folder". Here's my folder structure...

      Example 1 (deepest):
      - BIG DIRECTORY
      + Folder
      + Folder
      + Folder
      - Folder
      + Subdirectory
      + Subdirectory
      + Subdirectory
      - Subdirectory
      << Random Image File >>
      << Random Image File >>
      << Random Image File >>
      << Random Image File >>
      + Subdirectory
      + Subdirectory
      + Subdirectory
      + Subdirectory
      + Folder
      + Folder
      + Folder
      + Folder

      Example 2 (no subdirectories in Folder):
      - BIG DIRECTORY
      + Folder
      + Folder
      + Folder
      - Folder
      << Random Image File >>
      << Random Image File >>
      << Random Image File >>
      << Random Image File >>
      + Folder
      + Folder
      + Folder
      + Folder

      I hope you understand what I mean - I realize it's complex, which is why I need help. Should I consider using functions or variable functions???

        well, you could make your code a bit smaller and more reusable by using a recursive function to scan for the files.

        heres an example
        http://phpbuilder.com/board/showthread.php?s=&threadid=10290483

        you would need to modify that function a tiny bit.

        to pull a random element from an array, look at array_rand()

        its not too efficient to scan hundreds of directories to fetch a random file on every single page request. you could cache the results and just pick a random element on each page request.

        what i would do is :
        -scan all the directories for all files,
        -filter out all unwanted files
        -cache the results in a db or file

        then just pull a random element from the cached array using array_rand()

        set up a cron job to periodically execute the script that will scan your directories and update you cache. maybe run it every hour or whatever time interval is suitable.

          I completely hacked through this code to get it working. Can anyone clean it up for me? Please help rewrite if you know of any easier way...

          I'm especially confused with this part in my function:
          if(is_file($filepath)===true) {
          return $filepath;
          }

          <?php 
          /* Purpose of this code...
          1. Randomly selects a folder from the directory of my choice: i.e. MyPictures/Images
          2. Then randomly selects one image (all files, no subdirectories inside folder)
          3. If there ARE subdirectories, then randomly select one AND randomly select an image from that subdirectory.
          4. Could be coded for more than three levels deep and any combination of folders/files.
          */
          
          function randomimage() {
          // BEGIN CODE FOR STEP ONE...
          $dir = "MyPictures/Images";
          $dh  = opendir($dir);
          while (false !== ($foldername = readdir($dh))) {
          if ($foldername == "." || $foldername == ".." || $foldername == "Thumbs.db" || $foldername == "desktop.ini") {
          	}else{
          	$folders[] = $foldername;
          	}
          }
          // sort($folders);
          // print_r($folders);
          $intmin = 0;
          $intmax = count($folders)-1;
          $randfoldnum = rand($intmin, $intmax);
          $randomfoldname = $folders[$randfoldnum];
          
          $folderpath = $dir.'/'.$randomfoldname;
          // END CODE FOR STEP ONE...
          
          
          
          // BEGIN CODE FOR STEP TWO...
          $subdir = $dir.'/'.$randomfoldname;
          $subdh  = opendir($subdir);
          while (false !== ($filename = readdir($subdh))) {
          if ($filename == "." || $filename == ".." || $filename == "Thumbs.db" || $filename == "desktop.ini") {
          	}else{
          	$files[] = $filename;
          	}
          }
          
          $subintmin = 0;
          $subintmax = count($files)-1;
          $randfilesnum = rand($subintmin, $subintmax);
          $randomfilesname = $files[$randfilesnum];
          
          $filepath = $subdir.'/'.$randomfilesname;
          // END CODE FOR STEP TWO...
          
          
          
          if(is_file($filepath)===true) {
          return $filepath;
          }
          
          
          
          // BEGIN CODE FOR STEP THREE...
          $lastdir = $subdir.'/'.$randomfilesname;
          $lastdh  = opendir($lastdir);
          while (false !== ($imagename = readdir($lastdh))) {
          if ($imagename == "." || $imagename == ".." || $imagename == "Thumbs.db" || $imagename == "desktop.ini") {
          	}else{
          	$images[] = $imagename;
          	}
          }
          // sort($images);
          // print_r($images);
          $lastintmin = 0;
          $lastintmax = count($images)-1;
          $randimagesnum = rand($lastintmin, $lastintmax);
          $randomimagesname = $images[$randimagesnum];
          
          $subfilepath = $lastdir.'/'.$randomimagesname;
          // END CODE FOR STEP THREE...
          
          
          
          return $subfilepath;
          
          
          
          }
          
          
          $randomimage = randomimage();
          
          echo "<DIV align='center'><A HREF='$currentpage'><IMG SRC='$randomimage' ALT='$randomimage' BORDER='0'></A></DIV>";
          ?>
            Write a Reply...