Hi,

I need help with writing a piece of code that counts all the folders in a specific directory and prints the number, then counts image files in the same directory but located in all the fullsize sub folder of each gallery.

Example:

-images
-galleries
-gallery1
- fullsize
-thumbs
-gallery2
- fullsize
-thumbs
-gallery3
- fullsize
-thumbs

and so forth.

So far I have this much of the code:

<?php


$directory='images/galleries';


$count=0;


if ($handle = opendir($directory)) {
    ;

    
    while (false !== ($file = readdir($handle))) {
        ;

        
        $file_parts=explode('.',$file);
        $file_parts_counts=count($file_parts);
        $file_type_location=$file_parts_counts-1;
        $file_type=$file_parts[$file_type_location];
        
        
        $count=$count+1;

    }

    closedir($handle);
}
echo "Galleries: <b>$count</b>";
?>

There are 31 folders in the gallery folder and count returns 39.

Can someone please shed some light here.

Thank you very much.

Dani AI

Generated

For : the root cause is that your loop is counting every entry returned by the directory read, not just actual gallery folders. was on the right track — a more robust approach is to explicitly enumerate directories and then, for each gallery, only inspect its fullsize subfolder.

A compact, reliable way to count galleries is to use glob() with GLOB_ONLYDIR and count():

$dirs = glob('images/galleries/*', GLOB_ONLYDIR);
$galleryCount = count($dirs);

See the glob() docs for details: glob() manual.

To count images only inside each gallery's fullsize folder you can also use glob() with an extension list, or fall back to SPL iterators for case-insensitive or more complex rules:

$total = 0;
foreach ($dirs as $g) {
    $fs = $g . '/fullsize';
    if (!is_dir($fs)) continue;
    $imgs = glob($fs . '/*.{jpg,jpeg,png,gif}', GLOB_BRACE | GLOB_NOSORT);
    $total += count($imgs);
}

If GLOB_BRACE isn't available on your system or you need case-insensitive matching, use FilesystemIterator + RegexIterator and iterator_count() instead. See the SPL docs: FilesystemIterator and RegexIterator.

Extra tips: ignore OS artifacts like .DS_Store or thumbs.db, verify files with exif_imagetype() or finfo_file() if extensions are unreliable (exif_imagetype), and consider caching results if you have many galleries to avoid repeated filesystem scans.

Hi,

I need help with writing a piece of code that counts all the folders in a specific directory and prints the number, then counts image files in the same directory but located in all the fullsize sub folder of each gallery.

Example:

-images
-galleries
-gallery1
- fullsize
-thumbs
-gallery2
- fullsize
-thumbs
-gallery3
- fullsize
-thumbs

and so forth.

So far I have this much of the code:

<?php


$directory='images/galleries';


$count=0;


if ($handle = opendir($directory)) {
    ;

    
    while (false !== ($file = readdir($handle))) {
        ;

        
        $file_parts=explode('.',$file);
        $file_parts_counts=count($file_parts);
        $file_type_location=$file_parts_counts-1;
        $file_type=$file_parts[$file_type_location];
        
        
        $count=$count+1;

    }

    closedir($handle);
}
echo "Galleries: <b>$count</b>";
?>

There are 31 folders in the gallery folder and count returns 39.

Can someone please shed some light here.

Thank you very much.

You're counting the "images" folder as well. Each directory will have a . directory in it that points to the parent directory.

So you're counting the parent directory when you count the ".".

You probably want.

while (false !== ($file = readdir($handle))) {
        ;

        if ($file == '.' || $file == '..') {
          continue; // skip this file
        }
        
        $file_parts=explode('.',$file);
        $file_parts_counts=count($file_parts);
        $file_type_location=$file_parts_counts-1;
        $file_type=$file_parts[$file_type_location];
        
        
        $count=$count+1;

    }

Also to get the file extension on one line:

$file_type = substr($file, strrpos($file, '.')+1);

You can also abbreviate:

$count=$count+1;

to:

$count++;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.