Hi guys...

I want some help of how to call images from a folder....
Its like..v have a folder called images and it has many images in it.....but i need only 6 images to display in a php webpage...when the webpage is reloaded, it displays another images...its like its displaying random images.....But in one page, i would like to display only 6 images....So, when the reload or visit again, the images or randomised from the images folder.....can any one know how can it be done....
I m not sure abt the codes...


9 Years
Discussion Span
Last Post by Sâjjad
Featured Replies
  • This is something I just typed up. Not the same as the one I get to the other poster, but close. It will read a folder and get the images. Then randomize them and display in a paginated form. [code] <?php session_start(); $thispage = 'image.php'; $imageDir = 'icons/'; $perPage = … Read More


ok sounds pretty easy. how do you want the images displayed on the page? in a table?

Ya..in a table like in one row got 2 images...
so 3 rows and 2 columns...


i posted exactly same one as you want 3 days ago.


Im new to the forums here but was just wondering if there is any chance of being able to look at the code? im looking for something similar to this but all the examples i have found online are for only displaying 1 random image at time...

Cheers Ezy

		$arr[$i]="./img/images/".$i.".jpg"; //assigning the image location to the array $arr.
	$random=rand(0,6); //generating a random number
	echo "<img src=".$arr[$random].">";
//printing the image

Thats it :)


if thats the case, why not generate one more random number ?

		echo "<img src=".$arr[$random].">";
		echo "<img src=".$arr[$random_1].">";

This will do.. Don't you think ?


that looks like it could do the trick with abit of working... basically....

i want there to be a folder of images... the names of those images could be anything at all.... number letters.. mixtures... there will be no logical order to them..... i just need the code to randomly pick say 10 images... out of that folder... no image being displayed twice etc...

if there isnt enough ... say there is 4 images.. it will just display 4

if there is no images... maybe just a statement showing there is no images to display at this time...


finished the code; i sent you a private message with the url of the test script

Hi, I am fairly new to web design and am trying to do exactly what you helped the other person on this post do, is there any way I could also get ahold of that code? I would be very grateful. Thanx.



I am new to Php. Iam unable to write a php code for random images on a webpage that displays new image each time it is refreshed. Im having images in a folder in my college FTP. Can any one post a code for me in Php ? Coz im unable to make the changes to my php file....


This is something I just typed up. Not the same as the one I get to the other poster, but close.

It will read a folder and get the images. Then randomize them and display in a paginated form.



$thispage = 'image.php';
$imageDir = 'icons/';
$perPage = 10; //total images to display per page
$perRow = 2; //total images per row

if ( isset( $_GET['reset'] ) ) {
	unset( $_SESSION['chunks'] );

if ( !isset( $_SESSION['chunks'] ) ) {
	if ( is_dir( $imageDir ) ) {
		if ( $dir = opendir( $imageDir ) ) {
			while ( ( $file = readdir( $dir ) ) !== false ) {
				if ( $file !== '.' && $file !== '..' ) {
					$images[] = $file;
			closedir( $dir );
		else {
			die("Unable to read directory - {$imageDir}");
	shuffle( $images );
	$_SESSION['chunks'] = array_chunk( $images,$perPage );

$page = 1;
if ( isset( $_GET['page'] ) && $_GET['page'] > 0 ) {
	$page = (int) $_GET['page'];

$chunk =& $_SESSION['chunks'];
$total = count( $chunk );
if ( isset( $chunk[$page-1] ) ) {
	$images = $chunk[$page-1];
	$html = "<table cellspacing=\"0\" cellpadding=\"3\">\n\t<tr>\n";
	$i = 0;
	foreach( $images as $image ) {
		if ( $i == $perRow ) {
			$html .= "\t</tr>\n\t<tr>\n";
			$i = 0;
		$html .= "\t\t<td><img src=\"{$imageDir}{$image}\" /></td>\n";
	$html .= "\t<tr>\n";
	if ( $total > 1 ) {
		$nav = '';
		if ( $page > 1 ) {
			$nav .= "<a href=\"{$thispage}?page=1\">[FIRST]</a><a href=\"{$thispage}?page=" . ( $page - 1 ) . "\">[PREV]</a>";
		$i = 1;
		while( $i < $total + 1 ) {
			$nav .= "<a href=\"{$thispage}?page={$i}\">[{$i}]</a>";
		if ( $page < $total ) {
			$nav .= "<a href=\"{$thispage}?page=" . ( $page + 1 ) . "\">[NEXT]</a><a href=\"{$thispage}?page={$total}\">[LAST]</a>";
		$html .= "\t<tr>\n\t\t<td colspan=\"{$perRow}\">{$nav}</td>\n\t</tr>\n";
	$html .= "</table><br /><br /><a href=\"{$thispage}?reset=true\">Shuffle Images</a>";
	echo $html;


How can we make the image click able ?
wrt line 48 in your code

$html .= "\t\t<td><img src=\"{$imageDir}{$image}\" /></td>\n";

I tried

$html .= "\t\t<td><a href="MYSITE.com/?v={$image}"<img src=\"{$imageDir}{$image}\" /></td>\n";

But I got error..
can you please help me to make the images displayed clickable in this format

Thanks :D

Edited by poison23: n/a


Can I put this in an asp.net page?

I'm not sure where to put this php code.. ? Sorry

Edited by keltoid: n/a


Use simple function.

function RandImg($dir)
$images = glob($dir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);

$randomImage = $images[array_rand($images)];
return $randomImage;

$the_image = RandImg('images/tips/');
echo $the_image;
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.