PHP Gallery


#1

Hey all, I am working on this script for a PHP Image Gallery and I’m stuck! I got it to select the folders and images within folders and display and link them, but I don’t know how to get it to display only a certain number of images per page and have different pages for the images! Could someone help me out!? Here’s the script:

<?php
if (isset($_GET["page"])) {
	$dir = $_GET["page"] . "/";
	if (is_dir($dir)) {
		if ($handle = opendir('./' . $dir)) {
		   while (false !== ($file = readdir($handle))) {
			   if ($file != "." && $file != ".." && $file != "gallery.php" && $file != "Thumbs.db") {
			   	if (is_dir($file)) {
					echo "<a href="gallery.php?page=$dir$file">$file</a>n";
				} else {
					echo "<img src="$dir$file">";
				}
			   }
		   }
		   closedir($handle);
		   $total_images = count(glob('./' . $dir . '{*.gif,*.jpg,*.png}', GLOB_BRACE));
		   echo "Total Images:  " . $total_images;
		}
	} else {
		$dir2 = substr($dir, 0, -1);
		echo "<img src="$dir2">";
	}
} else {
	$dir = "";
	if ($handle = opendir('./' . $dir)) {
	   while (false !== ($file = readdir($handle))) {
		   if ($file != "." && $file != ".." && $file != "gallery.php" && $file != "Thumbs.db") {
			if (is_dir($file)) {
				echo "<a href="gallery.php?page=$dir$file">$file</a>n";
			} else {
				echo "<img src="$file">";
			}
		   }
	   }
	   closedir($handle);
	   $total_images = count(glob('./' . $dir . '{*.gif,*.jpg,*.png}', GLOB_BRACE));
	   echo "Total Images:  " . $total_images;
	}
}
?>

#2

I don’t work much with images or directories but maybe you could put the image names into a session array (maybe multidimensional if you want to track all the albums and subalbums in it - or just make new session arrays). then foreach page you just need a starting position in the array, loop through the array however many pictures you want to display.

And yeah - I am talking out my butt ;)