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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.