I\'m trying to make a site where users can submit photos, and then randomly view others photos one by one on another page. I have a directory called \"uploads\" where the pi
This single line of code displays one random image from the target directory.
Target directory: /images/
Image prefix: image_
Number of images in directory: 127
https://perishablepress.com/drop-dead-easy-random-images-via-php/
Drawbacks
images must be named sequentially (eg image_1.png
, image_2.png
, image_3.png
, etc).
you need to know how many images are in the directory in advance.
Alternatives
Perhaps there's a simple way to make this work with arbitrary image-names and file-count, so you don't have to rename or count your files.
Untested ideas:
/>
shuffle()
scanDir()
with rand(1,scanDir.length)