Answer the question
In order to leave comments, you need to log in
Script to randomly display a file on a website?
We need a script that would randomly display a file from a certain directory on the site.
Those. when visiting the site (or when reloading the page), each time a new file would be displayed, which would be randomly selected from a certain directory.
The file type can be: graphics, sound, video.
Are there ready-made solutions?
Answer the question
In order to leave comments, you need to log in
In php you can do something like this:
function randomFileFromDir($dir){
$scan = array();
$scan = scandir($dir);
$files = array_diff($scan, array('.', '.'));
return $scan[array_rand($scan)];
}
$randomFile = randomFileFromDir('someFolder');
$file_type = pathinfo($randomFile, PATHINFO_EXTENSION);
switch($type ){
case 'mp3';
echo 'Тут вывод плеера';
break;
case 'jpg':
case 'png':
echo 'Тут вывод картинки, если картинки , можно дополнить выборку выше';
break;
//...ну и так далее...
}
Let me add to the question:
let's say there is a directory "123", it contains files of different formats (jpg, mp3, mp4, well, etc.)
The script randomly selects, for example, jpg and it is necessary that this file be displayed on the site not just a filename. And, accordingly, if it is mp3 or mp4, then something like a player would be loaded so that you could immediately listen / view the file.
Here, I would like to find something ready in this regard ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question