all images in a directory

images php coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
swarups
New php-forum User
New php-forum User
Posts: 20
Joined: Tue Mar 06, 2012 11:05 pm

Tue Apr 09, 2013 3:32 am

Hi loriana,

I consider there is no db operation for this. I hope following code will help you.

Code: Select all

$directoryPath = 'Directory name'; //directory name where images are present
$per_page = 40;

//get the page number from url
if(isset($_GET['page_no'])){
   $page = $_GET['page_no'];
}
if($page == '' || $page < 1){
   $page = 1;
}


if (is_dir($directoryPath)) {
  if ($handle = opendir($directoryPath)) {
     while (false !== ($file = readdir($handle))) { 
	if ($file != '.' && $file != '..'){
	   $arrayDir[$arrayCnt] = $file;
	   $arrayCnt = $arrayCnt + 1;
	}
     }
     array_multisort($arrayDir);
     for ($i = 0; $i < ($page*$per_page); $i++) {
		//code to discplay images			
     }
  }
}


seandisanti
php-forum Fan User
php-forum Fan User
Posts: 973
Joined: Mon Oct 01, 2012 12:32 pm

Tue Apr 09, 2013 11:11 pm

i believe scandir is a better way to go. it will return an array of files and directories in a directory. Then you can use an iterator, or paginate it however you want. here's a video showing some SPL usage including iterators. http://jream.com/learning/videos/php-oo ... troduction

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 2 guests