Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

display random image in the same page?

Status
Not open for further replies.

hisham

IS-IT--Management
Nov 6, 2000
194
How can I display random image in the same page?
Every time some body requests a php page, this will display different image from the images folder. Hope someone can help.
Thanks in advance.
 
Code:
<?php
// Directory goes here relative to script (fs = file system; ws = web server directory)
$image_fs_dir = '/absolute/path/to/images'; // without trailing slash
$image_ws_dir = '/web/path/to/images/';  // with trailing slash
$href_link = '[URL unfurl="true"]http://www.ironorchid.com/clipart/';[/URL] // What your link points to (optional)

$handle = opendir($image_fs_dir);
while (false !== ($file = readdir($handle))) {
  if (preg_match(&quot;/\.(jpe?g|gif|png)$/i&quot;, $file)) {
    $filelist[] = $file;
  }
}
closedir ($handle);

srand((double)microtime()*1000000);
$picnum = rand(0, sizeof($filelist) - 1);

$content = '<div align=&quot;center&quot;>';
if ($href_link != '') {
  $content .= '<a href=&quot;' . $href_link . '&quot;><img src=&quot;' . $image_ws_dir . $filelist[$picnum] . '&quot; border=0></a>';
} else {
  $content .= '<img src=&quot;' . $image_ws_dir . $filelist[$picnum] . '&quot; border=0>';
}
$content .= '</div>';

?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top