Jurik Posted January 5, 2007 Share Posted January 5, 2007 Hi guys. I have a folder with a load of images in, all of which are named diffrently. They are all .jpg however so that makes things a little bit easier I was just wondering how I would go about setting up a peice of php code that will pick one of these images and display it. Basicaly a random image generator I have googled it but all the tutorials seem to only work if you name the images 1.jpg 2.jpg etc any help would be great. Quote Link to comment Share on other sites More sharing options...
taith Posted January 5, 2007 Share Posted January 5, 2007 $pictures=glob('images/*.jpg');echo $pictures[rand(0,count($pictures)-1)]; Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted January 5, 2007 Share Posted January 5, 2007 Another way:[code]<?php$pictures = glob('images/*.jpg');echo '<img src="' . $pictures[array_rand($pictures)] . '">';?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
Jurik Posted January 5, 2007 Author Share Posted January 5, 2007 ok I have tried both methods and for the first one it prints out '; ?> while the second example just displays blank, any ideas? Heres what ive done<?php $pictures=glob('PrizeGiving/thumbnails/*.jpg');echo $pictures[rand(0,count($pictures)-1)];?> Quote Link to comment Share on other sites More sharing options...
taith Posted January 5, 2007 Share Posted January 5, 2007 try print_r($pictures); is the array got anything in it? Quote Link to comment Share on other sites More sharing options...
obsidian Posted January 5, 2007 Share Posted January 5, 2007 First, you're declaring a relative path from the current file, so make sure your script is sitting in the folder containing the [i]PrizeGiving[/i] directory. Once you've done that, try printing out your glob() results and see if it's actually picked up your image names:[code]<?php// Make sure that your images are named appropriately and that you have// your case correct for your full path$pictures = glob("PrizeGiving/thumbnails/*.jpg");print_r($pictures);?>[/code]If the above code, does indeed collect all your image names, here's another alternative to get a random image:[code]<?php$pictures = glob("PrizeGiving/thumbnails/*.jpg");shuffle($pictures);echo "<img src=\"{$pictures[0]}\" />";?>[/code]Good luck! 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.