DaveLinger Posted July 24, 2006 Share Posted July 24, 2006 I'm working on a random background image selector... I'd like it to check the appropriate folder for all files (there would only be jpegs) and choose one filename and make it a variable. How is this accomplished? Quote Link to comment https://forums.phpfreaks.com/topic/15514-find-all-files-in-a-folder-choose-one-randomly/ Share on other sites More sharing options...
AV1611 Posted July 24, 2006 Share Posted July 24, 2006 create a list of files, then count the lines, then feed that into a random number gen then display the line that equals the results Quote Link to comment https://forums.phpfreaks.com/topic/15514-find-all-files-in-a-folder-choose-one-randomly/#findComment-63009 Share on other sites More sharing options...
DaveLinger Posted July 24, 2006 Author Share Posted July 24, 2006 well I mean I know how to randomly select from an array, just not how to find all files in a folder and put them into said array. Quote Link to comment https://forums.phpfreaks.com/topic/15514-find-all-files-in-a-folder-choose-one-randomly/#findComment-63012 Share on other sites More sharing options...
ryanlwh Posted July 24, 2006 Share Posted July 24, 2006 http://us2.php.net/manual/en/ref.dir.php Quote Link to comment https://forums.phpfreaks.com/topic/15514-find-all-files-in-a-folder-choose-one-randomly/#findComment-63013 Share on other sites More sharing options...
DaveLinger Posted July 24, 2006 Author Share Posted July 24, 2006 okay, it says that[code]<?php$dir = "/images/";// Open a known directory, and proceed to read its contentsif (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { echo "filename: $file : filetype: " . filetype($dir . $file) . "\n"; } closedir($dh); }}?> [/code]should display any files in the "images" directory, but echos nothing. script is located...domain www index.php test.php script.php images/ image.jpg image2.jpg image3.jpg Quote Link to comment https://forums.phpfreaks.com/topic/15514-find-all-files-in-a-folder-choose-one-randomly/#findComment-63017 Share on other sites More sharing options...
kenrbnsn Posted July 24, 2006 Share Posted July 24, 2006 You may also want to look at the glob() function (http://www.php.net/glob) which returns all the file names in directory as an array. Then use array_rand() to select one at random.[code]<?php$dir = glob("images/*.jpg");$rand_file = $dir[array_rand($dir)];echo $rand_file;?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/15514-find-all-files-in-a-folder-choose-one-randomly/#findComment-63024 Share on other sites More sharing options...
DaveLinger Posted July 24, 2006 Author Share Posted July 24, 2006 oh skeetthanks Quote Link to comment https://forums.phpfreaks.com/topic/15514-find-all-files-in-a-folder-choose-one-randomly/#findComment-63066 Share on other sites More sharing options...
Orio Posted July 24, 2006 Share Posted July 24, 2006 You should look into [url=http://www.php.net/manual/en/function.scandir.php]scandir()[/url] too :)Orio. Quote Link to comment https://forums.phpfreaks.com/topic/15514-find-all-files-in-a-folder-choose-one-randomly/#findComment-63076 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.