Code Snippet: Get a random image from a directory.

This can be useful for banner ads.

function getRandomImage( ) {

$path = “”; // Replace with your path

$img_list = array();

$dir = opendir( $path );

// Get all the files in the directory

while( $img = readdir($dir) )
{
if( $img != “.” && $img != “..”)
{
$img_list[] = $img;
}
}

closedir($dir);

// Randomly select a file from the list
$rand_img = rand(0, count($img_list)-1 );

$rand_img = $img_list[$rand_img];

return $rand_img;
}

Posted in Uncategorized

Leave a Reply