Random Image Code
One of the things that makes me want to visit a site is knowing that I might see something different everytime, even if its just a visual change.
One of the sites I'm working on wanted to display a couple of pictures on their main page - I suggested that we expand on this so that different pictures were shown whenever someone visited the page. To allow this, I came up with this section of code.
<?php
$file_array = array();
$dir = opendir($_SERVER['DOCUMENT_ROOT']."/images/random");
while ($file = readdir($dir) ) {
if ($file != 'Thumbs.db' and substr($file, 0, 1) != '.' )
array_push($file_array, $file);
}
foreach (array_rand($file_array, 4) as $file) {
printf("\n",
$file_array[$file]);
}
?>
Using it is fairly straightforward - it looks for a directory on the website (I used /images/random) and it generates a list of all files in that folder and then displays 4 random files from the contents of the directory.
A couple of things to note though - firstly, it does no checking to ensure that the files are images and secondly, it assumes that all of the images have been pre-sized and displays them at a fixed size.