Jump to content

How to create a radom image displayer.


Jurik

Recommended Posts

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.
Link to comment
https://forums.phpfreaks.com/topic/32954-how-to-create-a-radom-image-displayer/
Share on other sites

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.