How to get random image from directory using PHP

php

Solution

$imagesDir = 'images/tips/';

$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);

$randomImage = $images[array_rand($images)]; // See comments

You can send a 2nd argument to `array_rand()` to get more than 1.

Problem

I have one directory called images/tips. Now in that directory I have many images which can change. I want the PHP script to read the directory, to find the images, and out of those images found to pick a random image. Any idea on how to do this?

Original source