Page 1 of 1

How to list multiple images automatically in an array?

Posted: Fri Jul 29, 2011 3:26 am
by sbaker
I have over 300 images in folder, but when clicking on the image it is repeating and showing the same first 20 images - it is never getting to the other 280 images in the folder.

This is the code I am currently using:

Code: Select all

<?php
$imageFolder = 'images';

// Get file names
// (do that manually if you want to save performance: $images[] = 'image1.png'; ...)
$images = array();
$dirh = opendir($imageFolder);
while( $file = readdir($dirh) ) {
    if( $file != "." && $file != ".." ) {
        $images[] = $file;
    }
}

$imageFileName = $images[rand(0, count($images)-1)];

echo '<img src="' . $imageFolder . '/' . $imageFileName . '" alt="" />';
I want to put all the images into an array and save it in a session, shuffle this array and loop through the array, that would allow to never view the same images anymore. Does anyone here know how to code it?