Page 1 of 1

random image (strange)

Posted: Tue Jan 13, 2004 3:34 pm
by basdog22
hi,

I am making a script to have a random image each time a page loads. I managed to create it like this:

Code: Select all

<?php
if (isset($op))
{
srand((double)microtime()*1000000);
$directory=opendir('./');
$pics=array();
while ($file = readdir($directory))
{
  if (!is_dir($file))
  {
    $pics[]=$file;
  }
}
closedir($directory);
$random_nums=array_rand($pics,2);
$final=$pics[$random_nums['0']];
echo "<img src="$final">";
}
?>
it works but i want it to be diplayed when i call it like this:

Code: Select all

&lt;img src="rndimg.php"&gt;
I tried :

Code: Select all

<?php
header("Content-type: image/gif");
header("filename=$final");
?>
but i can't figure out the good way.
Any ideas??

Posted: Tue Jan 13, 2004 3:56 pm
by uberpolak
I would use

Code: Select all

<?php
header('Location: ' . $final);
?>
EDIT: What I mean is, use that code in place of the echo command.

Posted: Tue Jan 13, 2004 4:08 pm
by basdog22
you Rock.. 8) 8) 8) 8)

Thanks.

Posted: Tue Jan 13, 2004 4:24 pm
by uberpolak
No, geology rocks. You're welcome.

Posted: Tue Jan 13, 2004 11:50 pm
by Nay
Another way is also:

Code: Select all

header("Content-type: image/gif"); 

readfile($final);
-Nay