franzwarning Posted March 10, 2011 Share Posted March 10, 2011 Hi, I have a list of images from a directory that are in order by their "id". How could I display one at a time on a page--starting from the beginning, and have it so the user can click on the image and go to the next one in line. Quote Link to comment https://forums.phpfreaks.com/topic/230281-images-from-a-directory/ Share on other sites More sharing options...
Mahngiel Posted March 11, 2011 Share Posted March 11, 2011 take a read into opendir and readdir. from there, you can just run a while loop for the list of files and put anchors around them. Quote Link to comment https://forums.phpfreaks.com/topic/230281-images-from-a-directory/#findComment-1185927 Share on other sites More sharing options...
jcbones Posted March 11, 2011 Share Posted March 11, 2011 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. ?> Quote Link to comment https://forums.phpfreaks.com/topic/230281-images-from-a-directory/#findComment-1185974 Share on other sites More sharing options...
franzwarning Posted March 11, 2011 Author Share Posted March 11, 2011 Wow this is amazing thanks a ton. Quote Link to comment https://forums.phpfreaks.com/topic/230281-images-from-a-directory/#findComment-1186017 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.