random image (strange)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
basdog22
Forum Contributor
Posts: 158
Joined: Sun Nov 30, 2003 3:03 pm
Location: Greece

random image (strange)

Post 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??
User avatar
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

Post 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.
basdog22
Forum Contributor
Posts: 158
Joined: Sun Nov 30, 2003 3:03 pm
Location: Greece

Post by basdog22 »

you Rock.. 8) 8) 8) 8)

Thanks.
User avatar
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

Post by uberpolak »

No, geology rocks. You're welcome.
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Another way is also:

Code: Select all

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

readfile($final);
-Nay
Post Reply