I have some code shown below that is working to randomly show some images on my web page.
Right now it does the follwing:
- Reads the chosen directory
- matches filetype to a given array
- loads the matches into a picture array variable
- shuffles the picture array
- then outputs the image to the web page
So that works fine, but the pictures will only change when the page refreshes. Which is the way I have always done it before, so that works ok.
But - just to take it one step further, how could I add some sort of timer to force a page to refresh the screen which would in turn reload the images in xx seconds? Or would there be some better way to create a slide show type effect?
Code:
<?php
function file_type($file)
{
$path_chunks = explode("/", $file);
$thefile = $path_chunks[count($path_chunks) - 1];
$dotpos = strrpos($thefile, ".");
return strtolower(substr($thefile, $dotpos + 1));
}
$path = "assets/images/Deschutes/";
$file_types = array('jpeg', 'jpg', 'ico', 'png', 'gif', 'bmp', 'html');
$p = opendir($path);
while (false !== ($filename = readdir($p)))
{
$files[] = $filename;
}
sort($files);
foreach ($files as $file)
{
$extension = file_type($file);
if($file != '.' && $file != '..' && array_search($extension, $file_types) !== false)
{
$file_count++;
$picturesArray[] = $file;
/*echo '<a href="'.$path.$file.'">'.$file.'</a> <br/>';*/
}
}
srand((float) microtime() * 10000000);
shuffle($picturesArray);
echo "<img src=\"$path"."$picturesArray[0]\"";
echo " alt=\"Deschutes River, Oregon\" >";
?>