• home
  • forum
  • my
  • kt
  • download
  • PHP print random image from a folder

    Author: 2007-08-20 10:17:07 From:

    This is a tutorial that will show you how to create the simplest banner rotate system. The images are stored in a folder and are randomly displayed.

    The source code is posted below, with comments and it is very easy to understand.

     

    Note: For the images to be able do display,the folder that contains them has to have at least read and execute rights for everybody (555) and the images at least read for everybody (444).

    <?
    $imglist='';
    //$img_folder is the variable that holds the path to the banner images. Mine is images/tutorials/
    // see that you don't forget about the "/" at the end

    $img_folder = "images/tutorials/";

    mt_srand((double)microtime()*1000);

    //use the directory class
    $imgs = dir($img_folder);

    //read all files from the directory, checks if are images and ads them to a list (see below how to display flash banners)
    while ($file = $imgs->read()) {
    if (eregi("gif", $file) || eregi("jpg", $file) || eregi("png", $file))
    $imglist .= "$file ";

    } closedir($imgs->handle);

    //put all images into an array
    $imglist = explode(" ", $imglist);
    $no = sizeof($imglist)-2;

    //generate a random number between 0 and the number of images
    $random = mt_rand(0, $no);
    $image = $imglist[$random];

    //display image
    echo '<img src="'.$img_folder.$image.'" border=0>';
    ?>

    If you want to display flash banners, then you have to change the code a bit:

    The line that checks the right extension of files becomes:

    if (eregi("swf", $file))

    And the display image code:

    echo '<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"
    codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0"
    width="468" height="60">
    <param name="movie" value="'.$img_folder.$image.'">view/30/51/
    <param name="quality" value="high">
    <embed src="'.$img_folder.$image.'" quality="high"
    pluginspage="http://www.macromedia.com/go/getflashplayer"
    type="application/x-shockwave-flash" width="468"
    height="60"></embed></object>';

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (10)
      Flash and PHP (4)
      Form Processing (7)
      Guestbooks (8)
      Image Manipulation (3)
      Installing PHP (5)
      Introduction to PHP (9)
      Link Indexing (6)
      Mailing List Management (8)
      Miscellaneous (10)
      Networking (6)
      News Publishing (6)
      OOP (8)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (5)
      Postcards (0)
      Randomizing (8)
      Redirection (8)
      Searching (6)
      Security (6)
      Site Navigation (7)
      User Authentication (10)
      WAP and WML (7)
      Web Fetching (0)
      Web Traffic Analysis (11)
      XML and PHP (0)

    New

    Hot