This custom PHP function accepts image directory path as parameter and returns one random image from it.
Function
function randomImage($dir) { $files = scandir($dir); if (($key = array_search('.', $files)) !== false) { unset($files[$key]); } if (($key = array_search('..', $files)) !== false) { unset($files[$key]); } $file = array_rand($files); return $files[$file]; }
Calling The Function
echo randomImage('c:/wamp/www/Blog/public/photos/people/');
Output
spanish-girl.png
Leave A comment
Email address is optional and will not be published. Only add email address if you want a reply from blog author.
Please fill required fields marked with *