busnut Posted October 9, 2010 Share Posted October 9, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/215471-random-photo-from-all-sub-folders/ Share on other sites More sharing options...
BillyBoB Posted October 9, 2010 Share Posted October 9, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/215471-random-photo-from-all-sub-folders/#findComment-1120460 Share on other sites More sharing options...
kenrbnsn Posted October 9, 2010 Share Posted October 9, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/215471-random-photo-from-all-sub-folders/#findComment-1120461 Share on other sites More sharing options...
busnut Posted October 9, 2010 Author Share Posted October 9, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/215471-random-photo-from-all-sub-folders/#findComment-1120472 Share on other sites More sharing options...
Alex Posted October 9, 2010 Share Posted October 9, 2010 Personally, I'd probably use the RecursiveDirectoryIterator. Quote Link to comment https://forums.phpfreaks.com/topic/215471-random-photo-from-all-sub-folders/#findComment-1120479 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.