Jump to content

random photo from all sub folders


busnut

Recommended Posts

G'day, on the home page of a website i'm building, i'm wanting to have a random photo that looks at all the subfolders of the gallery directory, but the best I can achieve is just one subfolder.

 

Here is the code, if anyone can assist, that'll be appreciated.

 

<?php 
function getRandomFromArray($ar) { 
    mt_srand( (double)microtime() * 1000000 ); 
    $num = array_rand($ar); 
    return $ar[$num]; 
} 

function getImagesFromDir($path) { 
    $images = array(); 
    if ( $img_dir = @opendir($path) ) { 
        while ( false !== ($img_file = readdir($img_dir)) ) { 
            // checks for gif, jpg, png 
            if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) { 
                $images[] = $img_file; 
            } 
        } 
        closedir($img_dir); 
    } 
    return $images; 
} 

$root = ''; 
// If images not in sub directory of current directory specify root  
//$root = $_SERVER['DOCUMENT_ROOT']; 

$path = 'gallery/topic1/';

// Obtain list of images from directory  
$imgList = getImagesFromDir($root . $path); 

$img = getRandomFromArray($imgList); 

?>
<center><a href="?GoTo=Photo Gallery"><img src="<?php echo $path . $img ?>" alt="" height="267" width="400"/></a></center>

Link to comment
https://forums.phpfreaks.com/topic/215471-random-photo-from-all-sub-folders/
Share on other sites

Correct me if I'm wrong but wouldn't it work if you just opened the directories in a loop like:

 

<?php 
function getRandomFromArray($ar) { 
    mt_srand( (double)microtime() * 1000000 ); 
    $num = array_rand($ar); 
    return $ar[$num]; 
} 

function getImagesFromDir($path) { 
    $images = array(); 
    while ( $img_dir = @opendir($path) ) { 
        while ( false !== ($img_file = readdir($img_dir)) ) { 
            // checks for gif, jpg, png 
            if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) { 
                $images[] = $img_file; 
            } 
        } 
        closedir($img_dir); 
    } 
    return $images; 
} 

$root = ''; 
// If images not in sub directory of current directory specify root  
//$root = $_SERVER['DOCUMENT_ROOT']; 

$path = 'gallery/topic1/';

// Obtain list of images from directory  
$imgList = getImagesFromDir($root . $path); 

$img = getRandomFromArray($imgList); 

?>
<center><a href="?GoTo=Photo Gallery"><img src="<?php echo $path . $img ?>" alt="" height="267" width="400"/></a></center>

You need to use recursion to get all the images from the subdirectories. Replace your getImagesFromDir function with this:

<?php

function getImagesFromDir($dir) {
  $cur_level_dirs = glob($dir . '/*', GLOB_ONLYDIR);
  $cur_level_files = glob($dir . '/*.{jpg,gif,png}',GLOB_BRACE);
  foreach ($cur_level_dirs as $nxt) {
     $cur_level_files = array_merge($cur_level_files,getImagesFromDir($nxt));
  }
  return ($cur_level_files);
}
?>

 

Ken

Worked perfectly, with a small alteration:

 

<?php 
function getRandomFromArray($ar) { 
    mt_srand( (double)microtime() * 1000000 ); 
    $num = array_rand($ar); 
    return $ar[$num]; 
} 

function getImagesFromDir($dir) {
  $cur_level_dirs = glob($dir . '/*', GLOB_ONLYDIR);
  $cur_level_files = glob($dir . '/*.{jpg,gif,png}',GLOB_BRACE);
  foreach ($cur_level_dirs as $nxt) {
     $cur_level_files = array_merge($cur_level_files,getImagesFromDir($nxt));
  }
  return ($cur_level_files);
}

$root = 'gallery/'; 
// If images not in sub directory of current directory specify root  
//$root = $_SERVER['DOCUMENT_ROOT']; 

// Obtain list of images from directory  
$imgList = getImagesFromDir($root); 

$img = getRandomFromArray($imgList); 

?>

Thanks for the assistance, much appreciated :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.