Jump to content

Fetching image from directory


mamoosh

Recommended Posts

Hi everyone! I have this script that fetches images from a directory and shows them on one page. I was wondering if there is a way to have it list them in pages. For example, I want it to show 50 images per page, rather than echoing everything I have. Thanks in advance.

 

Here's the script I'm using:

 

 

<?php
$dir = 'images';

   $dirHandle = opendir($dir);
   $count = -1;
   $returnstr = "";
   while ($file = readdir($dirHandle)) {

      if(!is_dir($file) && strpos($file, '.jpg')>0) {

         $returnstr .= '<img src="'.$file.'" /><br />';
      }
   } 
   $returnstr .= ' ';
   echo $returnstr;
   closedir($dirHandle);
?>

 

P.S. can I do this:

 

<?php
      if(!is_dir($file) && strpos($file, '.jpg'||'.png'||'.gif')>0) {
?>

Link to comment
https://forums.phpfreaks.com/topic/96811-fetching-image-from-directory/
Share on other sites

read the whole directory into an session array once (check for it to make sure you don't keep doing it).

 

Then create a page variable that gets sent based on where you are

 

Then pick up the page variable and depending on where what page you are on, you'll get served up the correct files.

 

let me know if you need a quick example... hoping that just suggesting to you to put the thing in an array and use a page number system will suffice.

read the whole directory into an session array once (check for it to make sure you don't keep doing it).

 

Then create a page variable that gets sent based on where you are

 

Then pick up the page variable and depending on where what page you are on, you'll get served up the correct files.

 

let me know if you need a quick example... hoping that just suggesting to you to put the thing in an array and use a page number system will suffice.

 

Thank you very much, but if it's not too much to ask, can you please provide that example. I'm a newbie.  :P Thank you again.

I apologize for double posting, but I couldn't find an Edit button. Anywho...

 

Someone tried to help me with the code, however I'm getting errors like

 

unexpected T_VARIABLE on line 2

 

<?php

if(!isset($_SESSION["page"])
$_SESSION["page"]=1;

$dir = 'images';
$dirHandle = opendir($dir);
$count = -1;
$returnstr = "";
$localImageCount=0;
$ImagesPerPage=50;
while ($file = readdir($dirHandle)) {

if(!is_dir($file) && strpos($file, '.jpg')>0&&(localImageCount>..
($ImagesPerPage*$_SESSION..
["Page"]-1)&& localImageCount>(..
$ImagesPerPage*$_SESSION["Page"]))) {

$returnstr .= '<img src="'.$file.'" /><br />';

localImageCount++;
}
}
$returnstr .= ' ';
echo $returnstr;
closedir($dirHandle);
$_SESSION["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.