Display Images in a Folder (Randomly)

Discussion in 'PHP' started by FishSword, Feb 2, 2009.

  1. #1
    Hiya,

    I have a folder full of images. Upon a page refresh, how do you randomly display one of the images, so if a new image is added, that too will automatically randomly be displayed?
     
    FishSword, Feb 2, 2009 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    <?php
    
    $image_extensions = array( 'php' );
    $directory = './';
    
    /* *** */
    
    chdir($directory);
    
    $random_image = $images[array_rand($images = glob('*.{' . implode(',', array_values($image_extensions)) . '}', GLOB_BRACE), 1)];
    
    echo $random_image;
    PHP:
     
    Danltn, Feb 2, 2009 IP