Jump to content

Images from a directory


franzwarning

Recommended Posts

Something like:

<?php
session_start();

$dir = 'path/to/image/directory'; //<<<---- Path to your images, NO TRAILING SLASH.


if(!isset($_SESSION['images'])) { //If the image array IS NOT saved in sessions
if ($handle = opendir($dir)) { //Open the image directory.
	while (false !== ($file = readdir($handle))) { //while there are files to read.
		if($file != "." && $file != "..") { //if the file isn't current dir, or above dir.
		   $parts = explode('.',$file); //split the file on a period.			   
		   $c = count($parts); //count the parts array.
		   $ext = $parts[$c-1]; //ext will reside as the last value of the array.
		   if(strtolower($ext) == 'png' || strtolower($ext) == 'jpg' || strtolower($ext) == 'gif') { //if the ext is image type.				
				$images[] = $file; //save it to the image array.
			}
		}
	}
	closedir($handle); //close the directory.
}
sort($images);
$_SESSION['images'] = $images; //write the image array to a session variable.
} //this closes the if block, the above code will only run when the page is first open.

//Below is the displaying of the images, if you add images to the directory, you MUST close the browser window for
//this script to pick them up.

if(!isset($_SESSION['nextimage'])) { //if next image is NOT in the session array.
$next = $_SESSION['images'][1]; //next image will be the second image in the image array.
$current = $_SESSION['images'][0]; //current image will be the first image in the array.
}
else { //if next image is in the session array.
if(isset($_GET['next'])) { //and next is in the url bar.
	$current = $_SESSION['nextimage']; //current image is changed to the one held in Sessions nextimage.
}
elseif(isset($_GET['prev'])) { //or if prev is in the url bar,
	$current = $_SESSION['previousimage']; //we go to the previousimage in our sessions array.
}	
}
$keys = array_keys($_SESSION['images'],$current); //find our array key of the current pic.
$n = $keys[0]; //the key will reside in the first place of the keys array.
$next = (array_key_exists($n+1,$_SESSION['images'])) ? $_SESSION['images'][$n+1] : $_SESSION['images'][0]; //if the next array key exists in the images array, set the next image, if it don't the first image is next.
$previous = (array_key_exists($n-1,$_SESSION['images'])) ? $_SESSION['images'][$n-1] : end($_SESSION['images']); //if the previous key exists in the images array, set the previous image to it, if it don't set previous to the last value of the image array.

$_SESSION['nextimage'] = $next; //write the next to the session array.
$_SESSION['previousimage'] = $previous; //write the previous to the session array.
echo '<a href="?next=1"><img src="' . $dir . '/' . $current . '" alt="images" /></a><br />' . "\n"; // print the image element to the page.

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.