jacko310592 Posted November 29, 2009 Share Posted November 29, 2009 hey guys can someone please suggest a way in which the following code can be edited so it only picks up .jpg files, and so it only shows 20 of them which where found, in a randomized order <?php $files = new RecursiveDirectoryIterator("./gallery"); foreach(new RecursiveIteratorIterator($files) as $files) { $file = basename($files); echo ''.$file.'<br/>'; } ?> thanks everyone Quote Link to comment https://forums.phpfreaks.com/topic/183334-show-random-images/ Share on other sites More sharing options...
Alex Posted November 29, 2009 Share Posted November 29, 2009 Why not use glob. $files = glob('*.jpg'); array_splice(shuffle($files), 20); foreach($files as $file) { echo "$file<br />\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/183334-show-random-images/#findComment-967690 Share on other sites More sharing options...
jacko310592 Posted November 29, 2009 Author Share Posted November 29, 2009 unfortunately i dont think its possible to use glob because the directory 'gallery' is just the main dir, all the images are within sub-directories under 'gallery. sorry i forgot to mention that. but thats for the responce (: Quote Link to comment https://forums.phpfreaks.com/topic/183334-show-random-images/#findComment-967691 Share on other sites More sharing options...
Alex Posted November 29, 2009 Share Posted November 29, 2009 I'm not sure if this class implements an easier way to do this, there isn't documentation about it in the manual, but I don't think so. Something like this will work: function grab_files($start_dir, $ext) { $dir = new RecursiveDirectoryIterator($start_dir); foreach($dir->getChildren() as $file) if(is_dir($file)) $files = array_merge((array) $files, grab_files($file, $ext)); else if(pathinfo($file, PATHINFO_EXTENSION) == $ext) $files[] = (string) $file; return $files; } $files = grab_files('../gallery', 'jpg'); array_splice(shuffle($files), 20); foreach($files as $file) { echo $file . '<br />'; } Edit: Added the 20 file limit and randomization, forgot you wanted that as well. Quote Link to comment https://forums.phpfreaks.com/topic/183334-show-random-images/#findComment-967707 Share on other sites More sharing options...
jacko310592 Posted November 29, 2009 Author Share Posted November 29, 2009 i got this error msg multiple times: Warning: array_merge() [function.array-merge]: Argument #1 is not an array in http:\\www.mysite.com\index.php on line 62 any ideas? thanks for the helping Quote Link to comment https://forums.phpfreaks.com/topic/183334-show-random-images/#findComment-967718 Share on other sites More sharing options...
jacko310592 Posted November 29, 2009 Author Share Posted November 29, 2009 by the way- its refaring to this line: $files = array_merge($files, grab_files($file, $ext)); Quote Link to comment https://forums.phpfreaks.com/topic/183334-show-random-images/#findComment-967721 Share on other sites More sharing options...
Alex Posted November 29, 2009 Share Posted November 29, 2009 I actually edited my post before you said that because I noticed that design flaw myself (if the first file found is a directory that will happen). Copy the code that's there now and it should work. Quote Link to comment https://forums.phpfreaks.com/topic/183334-show-random-images/#findComment-967723 Share on other sites More sharing options...
jacko310592 Posted November 29, 2009 Author Share Posted November 29, 2009 i now have all my images being listed instead of just 20, with this error at the top: Warning: array_splice() expects parameter 1 to be array, boolean given in E:\xampp\htdocs\Katies Site 1.0\index.php on line 64 line 64 refaring to "array_splice(shuffle($files), 20);" Quote Link to comment https://forums.phpfreaks.com/topic/183334-show-random-images/#findComment-967725 Share on other sites More sharing options...
Alex Posted November 29, 2009 Share Posted November 29, 2009 I should stop trying to do 23940748 things at once, sorry. function grab_files($start_dir, $ext) { $dir = new RecursiveDirectoryIterator($start_dir); $files = Array(); foreach($dir->getChildren() as $file) if(is_dir($file)) $files = array_merge($files, grab_files($file, $ext)); else if(pathinfo($file, PATHINFO_EXTENSION) == $ext) $files[] = (string) $file; return $files; } $files = grab_files('../gallery', 'jpg'); shuffle($files); array_splice($files, 20); foreach($files as $file) { echo $file . '<br />'; } Silly mistake. Quote Link to comment https://forums.phpfreaks.com/topic/183334-show-random-images/#findComment-967730 Share on other sites More sharing options...
jacko310592 Posted November 29, 2009 Author Share Posted November 29, 2009 thanks Alex works great (: Quote Link to comment https://forums.phpfreaks.com/topic/183334-show-random-images/#findComment-967733 Share on other sites More sharing options...
salathe Posted November 30, 2009 Share Posted November 30, 2009 It would probably be easier and more correct to make proper use of the recursible nature of the RecursiveDirectoryIterator; it would save manually recursing the grab_files function and a lot of array merging. Here is a version the same function with a couple of differences: 1. Recurses using the iterator rather than by calling the function multiple times 2. Does not need array_merge 3. Allows multiple file extensions and checks them case insensitively The method of getting random keys is different too; instead of shuffling the entire array (which might be huge) it uses array_rand function grab_files($start_dir, $ext) { $dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($start_dir)); $files = array(); // Force array of extensions and make them all lower-case if ( ! is_array($ext)) { $ext = (array) $ext; } $ext = array_unique(array_map('strtolower', $ext)); foreach($dir as $file) { // Skip anything that isn't a file if ( ! $file->isFile()) continue; // If the file has one of our desired extensions, add it to files array if (in_array(strtolower(pathinfo($file->getFilename(), PATHINFO_EXTENSION)), $ext)) { $files[] = $file->getPathname(); } } return $files; } // Grab all .jpg files recursively $files = grab_files('./gallery', 'jpg'); // Get random keys $keys = array_rand($files, min(20, count($files))); foreach($keys as $key) { echo $files[$key] . "<br>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/183334-show-random-images/#findComment-968070 Share on other sites More sharing options...
jacko310592 Posted November 30, 2009 Author Share Posted November 30, 2009 thanks salathe, i appreciate the help (: Quote Link to comment https://forums.phpfreaks.com/topic/183334-show-random-images/#findComment-968247 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.