20
Sep
Learn how to select random file from directory using PHP
If you want to make a site where users can submit files, and then randomly view one by one on another page then, you can use the following function. Here, I have a directory called “uploads” where the files are submitted.
Here, you can use glob or opendir() to get all files in a directory, and then take a random element from that array. A function like this would do it for you:
function random_pic($dir = 'uploads') { $files = glob($dir . '/*.*'); $file = array_rand($files); return $files[$file]; }
NOTE: it is faster opendir() function than glob() function.