I have a directory which contains several text files. What I'm trying to do is to randomly select one of the files and then display it. Here's what i got so far, but i still haven't managed to get it working. Any idea's? Thanks.
function random_pic($dir = 'wp-content\files')
$files = opendir($dir . '/*.txt');
$file = array_rand($files);
echo fgets($file) . "<br />";
scandir will put all elements in the directory into an array. Then use
array_rand to choose a random element from the array.
$dir = "/path/to/pictures/"; $dirarray = scandir( $dir ); unset($dirarray ); unset($dirarray ); $content = file_get_contents( $dir . $dirarray[array_rand($dirarray )] ); echo $content;
unset commands are to remove
.. from the array.
This would for example result in echoing