Page 1 of 1

Listing Dir of Images

Posted: Tue Jun 06, 2006 1:48 pm
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;
}

Posted: Tue Jun 06, 2006 1:50 pm
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;
}

Posted: Tue Jun 06, 2006 1:52 pm
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;
}

Posted: Tue Jun 06, 2006 1:56 pm
by l337_haxs
Thankyou very much gentlemen! :)