need help I am a neebe can´t see the image

Dear PHP-Programmer,

I have a PHP script which select randomly a image from a folder called" bilder", the script has the name sabine3.php and it is the following:

<?php function getRandomImage($dir,$type='random') { global $errors,$seed; if (is_dir($dir)) { $fd = opendir($dir); $images = array(); while (($part = @readdir($fd)) == true) { if ( eregi("(gif|jpg|png|jpeg)$",$part) ) { $images[] = $part; } } // adding this in case you want to return the image array if ($type == 'all') return $images; if ($seed !== true) { mt_srand ((double) microtime() * 1000000); $seed = true; } $key = mt_rand (0,sizeof($images)-1); return $dir . $images[$key]; } else { $errors[] = $dir.' is not a directory'; return false; } } $image = getRandomImage('bilder/'); echo "Random Image"; ?>

The script is OK but I want to create a HTM-page where I can show the random image, I wrote this HTM-Code:

Untitled Document sabine

But it doesn´t work, so how can I show the image???

Thank you,

frank

Hi Frank!

If you load the sabine3.php page directly in the browser, does it show a random image as expected?

If it does, replace the following line in your html

<img src="<? sabine3.php ?>" alt="sabine" />

with this

<?php sabine3.php ?>

because sabine3.php already outputs a “complete” img tag rather than just the path to the file (which would go in the src attribute of the img tag.

Alternatively, change the following line in your php

[php]echo “<img src=’$image’ alt=‘Random Image’ border=“0”>”;[/php]

to

[php]echo $image;[/php]

Also, you should avoid the short php opening tag “<?” -instead use the “<?php” tag.

Cheers!

If loading the sabine3.php file directly into the browser does not work either, are you getting any error messages?

Sponsor our Newsletter | Privacy Policy | Terms of Service