reptkeu Posted February 27, 2010 Share Posted February 27, 2010 Hello, I wanted to have a page on my website that displays lots of images randomly from a folder. I foundĀ this article and this this code which exactly does what I am looking for. Problem is: the image folder has to be called "random" and has to be located at in the ROOT directory. (Oh, and one more problem: I am a total newbie in coding, just some html basic knowledge.) I keep trying to modify the script to point toward the right location but it just won't work. Can you guys help? Quote Link to comment Share on other sites More sharing options...
alpine Posted February 27, 2010 Share Posted February 27, 2010 Play around with these examples $random1 = RandomFile("random"); $random1 = RandomFile("dir/myfolder"); $random1 = RandomFile("../path/to/myfolder"); Quote Link to comment Share on other sites More sharing options...
monkeytooth Posted February 27, 2010 Share Posted February 27, 2010 This should do exactly as you seek.. I was sitting bored one night and I had a spare domain that was sitting doing nothing.. So I decided to put a random image gen on it. Nothing special about this script. Ā http://www.uglygoods.com if you want to see it working, just refresh the page. a couple times.. Ā <?php function RandomFile($folder='', $extensions='.*'){ Ā Ā // fix path: Ā Ā $folder = trim($folder); Ā Ā $folder = ($folder == '') ? './' : $folder; Ā Ā // check folder: Ā Ā if (!is_dir($folder)){ die('invalid folder given!'); } Ā Ā // create files array Ā Ā $files = array(); Ā Ā // open directory Ā Ā if ($dir = @opendir($folder)){ Ā Ā Ā Ā // go trough all files: Ā Ā Ā Ā while($file = readdir($dir)){ Ā Ā Ā Ā Ā Ā if (!preg_match('/^\.+$/', $file) and Ā Ā Ā Ā Ā Ā Ā Ā preg_match('/\.('.$extensions.')$/', $file)){ Ā Ā Ā Ā Ā Ā Ā Ā // feed the array: Ā Ā Ā Ā Ā Ā Ā Ā $files[] = $file;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā // close directory Ā Ā Ā Ā closedir($dir);Ā Ā Ā Ā } Ā Ā else { Ā Ā Ā Ā die('Could not open the folder "'.$folder.'"'); Ā Ā } Ā Ā if (count($files) == 0){ Ā Ā Ā Ā die('No files where found :-('); Ā Ā } Ā Ā // seed random function: Ā Ā mt_srand((double)microtime()*1000000); Ā Ā // get an random index: Ā Ā $rand = mt_rand(0, count($files)-1); Ā Ā // check again: Ā Ā if (!isset($files[$rand])){ Ā Ā Ā Ā die('Array index was not found! very strange!'); Ā Ā } Ā Ā // return the random file: Ā Ā return $folder . $files[$rand]; } ?> <img src="<?php print RandomFile('img/','jpg|png|gif'); ?>" /> <img src="<?php print RandomFile('img/','jpg|png|gif'); ?>" /> <img src="<?php print RandomFile('img/','jpg|png|gif'); ?>" /> <?php //This Bit only returns a count of files in a given directory $dir = "img"; $file_count = -3; // Open a known directory, and proceed to read its contents if (is_dir($dir)) { Ā if ($dh = opendir($dir)) { Ā Ā while (($file = readdir($dh)) !== false) { Ā Ā Ā //echo "filename: $file : filetype: " . filetype($dir . $file) . "<br />";<br /> Ā Ā $file_count++; Ā Ā } Ā Ā closedir($dh); Ā } Ā else { echo "x"; } } else { echo "o"; } ?> Ā Quote Link to comment Share on other sites More sharing options...
reptkeu Posted February 27, 2010 Author Share Posted February 27, 2010 I replaced Ā $random1 = RandomFile("random"); while (!$random2 || $random2 == $random1) { $random2 = RandomFile("random"); } Ā with Ā $random1 = RandomFile("http://bertrandclerc.com/autres/random_images/random/"); while (!$random2 || $random2 == $random1) { $random2 = RandomFile("random"); } Ā But it doesn't work. Any idea? Quote Link to comment Share on other sites More sharing options...
reptkeu Posted February 28, 2010 Author Share Posted February 28, 2010 Why isn't it working? Quote Link to comment Share on other sites More sharing options...
Orionsbelter Posted February 28, 2010 Share Posted February 28, 2010 have you tried just putting the path to the directory? not the URL. Quote Link to comment Share on other sites More sharing options...
reptkeu Posted February 28, 2010 Author Share Posted February 28, 2010 This should do exactly as you seek.. I was sitting bored one night and I had a spare domain that was sitting doing nothing.. So I decided to put a random image gen on it. Nothing special about this script. Ā http://www.uglygoods.com if you want to see it working, just refresh the page. a couple times.. Ā Hi Monkeytooth. It works great! I couldn't even get the one I mentionned at first to work. And I think even if I did, it would have been a nightmare (I want to display dozens of pictures and with this first code I would have had to copy/paste/modify the three lines everytime I want an iamge to display. With yours, it's much easier and quicker (just copy/paste......) Thanks a lot for your help. Quote Link to comment Share on other sites More sharing options...
reptkeu Posted February 28, 2010 Author Share Posted February 28, 2010 Just one ;ast question: how do you include subfolders? Quote Link to comment Share on other sites More sharing options...
reptkeu Posted February 28, 2010 Author Share Posted February 28, 2010 Guys, here is the result: a "mood-board" generator. I think it looks cool. What do you think? Monkeytooth, if you want me to write credits let me know ;-) Quote Link to comment 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.