Listing Dir of Images

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
l337_haxs
Forum Newbie
Posts: 2
Joined: Tue Jun 06, 2006 1:42 pm

Listing Dir of Images

Post by l337_haxs »

I have got a directory of images and I am in the middle of listing them out but no matter how hard I try I keep getting . & .. in my array. I know I am missing something because I had this working previously in some older function I built but I need to ask the gurus what I am missing.

Thanks!

Below is my code

Code: Select all

function GetPictures($dir)
{
	$picturearray=array();
	if (is_dir($dir)) 
	{
		if ($dh = opendir($dir)) 
		{
			while (($file = readdir($dh)) !== false) 
			{	
				if(!strpos($file,'small_'))
				{
					$picturearray[]=$file; 
				}
			}
			closedir($dh);
		}
	}
	return $picturearray;
}
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

quick fix

Code: Select all

function GetPictures($dir)
{
   $picturearray=array();
   if (is_dir($dir))
   {
      if ($dh = opendir($dir))
      {
         while (($file = readdir($dh)) !== false)
         {   
            if(!strpos($file,'small_') && $file != '.' && $file != '..')
            {
               $picturearray[]=$file;
            }
         }
         closedir($dh);
      }
   }
   return $picturearray;
}
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

A couple of things before I give you the answer. Always read the manual first! All you have to do is look at readdir() in the manual, scroll down to the examples, and it's right there!

Also, when posting PHP code, surround it in the [ php ] [ /php ] tags instead of the code ones - it syntax highlights it.

Code: Select all

function GetPictures($dir)
{
   $picturearray=array();
   if (is_dir($dir))
   {
      if ($dh = opendir($dir))
      {
         while (($file = readdir($dh)) !== false)
         {   
            if(!strpos($file,'small_') && $file != '.' && $file != '..')
            {
               $picturearray[]=$file;
            }
         }
         closedir($dh);
      }
   }
   return $picturearray;
}
l337_haxs
Forum Newbie
Posts: 2
Joined: Tue Jun 06, 2006 1:42 pm

Post by l337_haxs »

Thankyou very much gentlemen! :)
Post Reply