php random image selection

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
You can't see it on my site at because I have just
been attempting it on wampserver but I have three different jpegs in a
row on the LHS and I am trying to implement a random image selector so
that each time a page is loaded you get three new and different images.
I have tried a couple of scripts for random images but they both
produced three times the same image each time the page was loaded.

Anyone know a script which will work?

Tim W

Re: php random image selection

On Thu, 16 Feb 2012 17:38:20 +0000, Tim W wrote:

Quoted text here. Click to load it

It's not that difficult to roll your own:

1) get the names of all the files in directory x that match any of *.gif,
*.jpg, *.png. Look up the php documentation for glob()

2) pick 3 at random. Look up the php documentation for array_rand()

3) display them in the html. Use echo.

Some string manipulation may be required. Glob() uses the filesystem
path. You might want to apply the basename() function to the three random
picks, and then prepend them with the path relevant to the webroot.

eg: url0 = " /" . basename($files[$keys[0]]);

These are the bits that you need to glue together, have fun with your
construction kit toy. ;)


Denis McMahon

Site Timeline