Listing Directories with images and without
Posted: Sat Sep 17, 2005 6:37 am
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.
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.
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);
}What is causing the extra insertions.