Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Random image in a web page via PHP?

Posted by enquirer (enquirer), 13 April 2004
How can I get an image selected at random from a directory to appear in my web page?

Posted by admin (Graham Ellis), 13 April 2004
Example ... (press "reload" or "refresh" for a new image) :

Sample code:
header("content-type: image/jpeg\ncache-control: no-store\n");

$dh = opendir("../../imagelib/iom");
$contains = array();

while ($content = readdir($dh)) {
       if (ereg('[[:digit:]]',$content)) {

$which = rand(0,count($contains)-1);

$fh = fopen("../../imagelib/iom/$contains[$which]","r");

$data = fread($fh,250000);
print ($data)


a) Offers a random file with a name containing a digit (but you could change this)

b) Image size limited to 0.25 Mbytes which should be PLENTY!

c) Use of "no-store" in the header to prevent the image being cached or stored.  More efficient otherwise, but it means your visitors may end up caching the page and not see it change at each refresh.

d) Use the name of the PHP script in the SRC= attribute of the IMG tag, rather than the name of any one image

Posted by admin (Graham Ellis), 6 September 2004

Posted by neogen (neogen), 26 January 2005
what is wrong with my code?


$conn = mysql_connect("localhost", "root", "") OR DIE (mysql_error());
@mysql_select_db ("image", $conn) OR DIE (mysql_error());

$sql = "SELECT * FROM image WHERE image_id=". $_GET['iid'] ."";
$result = mysql_query ($sql, $conn);
if (mysql_num_rows ($result)>0) {
 $row = @mysql_fetch_array ($result);
     $imageId = $row["image_id"];
 $image_type = $row["image_type"];
 $image = $row["image"];

header("content-type: $image_type \n cache-control: no-store\n");

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="files/css/styles.css" rel="stylesheet" type="text/css" />

<table width="200" border="0" align="center" cellpadding="0" cellspacing="4">
   <td><table border="0" cellpadding="0" cellspacing="0" class="widthColor">

<?PHP print "<img src=\"index.php?iid=". $_GET['iid'] ."\">"; ?>

Posted by neogen (neogen), 26 January 2005
sorry for the rushed question...

My images doesn't show up and I have not a clue as to why... can anyone shed some light on my situation?

Posted by admin (Graham Ellis), 26 January 2005
on 01/26/05 at 03:50:10, neogen wrote:
My images doesn't show up and I have not a clue as to why... can anyone shed some light on my situation?

Yes. You're telling the browser that the stream of data it's about to receive is of type $image_type, and then you proceed to send it HTML (text/html).

If you're looking to include a selected image within a page, you'll do better if you use two distinct URLs.  The first one (the one that you point your browser at) is a text/html (no header function call needed) and includes the img tag.  The second one (URL specified within the src attribute of the img tag) should be of the type you've got from that database and the only output should be the actual image content; even a spurious new line can result in no image appearing.

My sample reproduced earlier in this thread is of the second script of a pair line this - the first is just a plain ole web page with an image tag as described in note (d).

As an aside - the the @ to supress errors in front of the mysql_fetch_array?  It looks as if you might simply be masking database problems with this - if there is an error, so simply go on and use nulls ....

Posted by neogen (neogen), 26 January 2005
Thx for your help... Works perfectly now

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: • WEB: • SKYPE: wellho