Get Random File from a Directory in PHP
To get a random file from a specified directory, use the glob()
, realpath()
and array_rand()
functions.
Remember to specify the directory you want to search inside the realpath()
function.
Example:
<?php
$allFiles = glob(realpath('images') . '/*.*');
$file = array_rand($allFiles);
echo $allFiles[$file];
?>