Random forum avatars - part two
Tonight, Twitter user Chris Smith suggested using redirect headers instead of using GD to render the image.
This didn't even occur to me, when I started with this idea. It is much more elegant and does not need the GD library. Continue reading to see the new code!
On the outside, the script works exactly the same as the other one.
This didn't even occur to me, when I started with this idea. It is much more elegant and does not need the GD library. Continue reading to see the new code!
Code:
<?
//define the path as relative
$path = "./";
//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");
$avatars = array();
//running the while loop
while ($file = readdir($dir_handle))
{
if(preg_match('~[^/]*\.jpg$~iD', $file))
{
$avatars[] = $file;
}
}
$rand_keys=array_rand($avatars,1);
closedir($dir_handle);
header('Location: http://<URL to script>/'.$avatars[$rand_keys]);
?> On the outside, the script works exactly the same as the other one.