Select random file from directory in PHP
php random filename generator
function random_string($length) {
$key = '';
$keys = array_merge(range(0, 9), range('a', 'z'));
for ($i = 0; $i < $length; $i++) {
$key .= $keys[array_rand($keys)];
}
return $key;
}
//
echo random_string(50);
Select a random file from directory. This can be useful if you want to display a random image on your website. For an example a background image randomly from a directory in your files.
$files = glob(realpath('../img') . '/*.*');
$file = array_rand($files);
echo $files[$file];