Listing Directories with images and without

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
robjime
Forum Commoner
Posts: 46
Joined: Sat Apr 03, 2004 12:26 pm
Location: the RO

Listing Directories with images and without

Post by robjime »

I have this function which if $type = 0, then it lists all the directories in a specified folder. If $type = 1 then it lists all the dirs in the specified folder that have image files within them.

Code: Select all

function list_dirs($path, $type)
{
   $list = scandir($path);
   $dir_list = array();
   foreach ($list as $number => $filename){
   
       if ( $filename !== '.' && $filename !== '..' && is_dir("$path/$filename") ){
       
           
           $dir = $filename;
		  
		   $ext = array("jpg", "jpeg", "gif");
			if($type == "0") {
					   $dir_list[] = $dir;
			} 
	// only include dir if it contains images
			else if($type == '1') {
				if($handle = opendir("$path/$dir")) { 
			       while(false !== ($file = readdir($handle))){
					  for($i=0;$i<sizeof($ext);$i++) {
						   if(strstr($file, ".".$ext[$i]))  {
							 $dir_list[] = $dir;
							 $pics = '1';
						   } 
						  
					   }  
						   
					}
				}closedir($handle);
       
   			}
         
	    
        
		
		} 
  }
   return($dir_list);
  
}
The problem is that when I use type =1. I have 2 images in folder 1 and 1 image in folder 2. So the script puts folder 1 into the $dir_list array 4 times. And the script puts folder 2 into the array 2 times.
What is causing the extra insertions.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

Code: Select all

$pics == 0;
while(false !== ($file = readdir($handle))){ 
     if ($pics == 1){
	break;//if an image is found in a folder; dont search for further images
     }
     for($i=0;$i<sizeof($ext);$i++) { 
           if(strstr($file, ".".$ext[$i]))  { 
              $dir_list[] = $dir; 
              $pics = '1'; 
	      break; //if found, dont search for other extensions
           } 
                           
    }   
                            
}
robjime
Forum Commoner
Posts: 46
Joined: Sat Apr 03, 2004 12:26 pm
Location: the RO

Post by robjime »

the problem still persists,

Code: Select all

function list_dirs($path, $type)
{
   $list = scandir($path);
   $dir_list = array();
   foreach ($list as $number => $filename){
   
       if ( $filename !== '.' && $filename !== '..' && is_dir("$path/$filename") ){
       
           
           $dir = $filename;
		  
		   $ext = array("jpg", "jpeg", "gif");
			if($type == "0") {
					   $dir_list[] = $dir;
			} 
	// only include dir if it contains images
			else if($type == '1') {
				if($handle = opendir("$path/$dir")) { 
			       $pics == 0; 
						while(false !== ($file = readdir($handle))){ 
							 if ($pics == 1){ 
							break;//if an image is found in a folder; dont search for further images 
							 } 
							 for($i=0;$i<sizeof($ext);$i++) { 
								   if(strstr($file, ".".$ext[$i]))  { 
									  $dir_list[] = $dir; 
									  $pics == 1; 
								  break; //if found, dont search for other extensions 
								   } 
													
							}    
													 
						} 
				}closedir($handle);
       
   			}
         
	    
        
		
		} 
  }
   return($dir_list);
  
}
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

Code: Select all

$count = 0;//count the number of times files matched.
while(false !== ($file = readdir($handle))){ 
	if ($pics == 1){ 
		break;//if an image is found in a folder; dont search for further images 
	} 
	for($i=0;$i<sizeof($ext);$i++) { 
		if(strstr($file, ".".$ext[$i])) {
			echo $file."<br />";
			//if(!in_array($dir_list)) $dir_list[] = $dir; //if you can't find the problem use this line
			dir_list[] = $dir; //if you uncomment the above line, then comment this line
			echo $count++."<br />";
			$pics == 1; 
			break; //if found, dont search for other extensions
		} 
                                                    
	}     
                                                     
}
robjime
Forum Commoner
Posts: 46
Joined: Sat Apr 03, 2004 12:26 pm
Location: the RO

Post by robjime »

Thanks a lot!!

Code: Select all

if(!in_array($dir, $dir_list)) $dir_list[] = $dir;
This line solved it.
Post Reply