tags:

views:

46

answers:

3

I have a Javascript object like this:

var count = {
            table:19,
            people:39,
            places_details:84,
            story_1:18,
            story_2:6,
            story_3:11
            }

Each item (table, people, etc.) is a directory within my graphics/ directory at the site root. I would like to use PHP to supply the numeric values by counting the JPG images in the corresponding directory. I imagine something like this:

ar count = {
           table: <?php count(dir("table")) ?>,
           people: <?php count(dir("people")) ?>,
           places_details: <?php count(dir("places_details")) ?>,
           story_1: <?php count(dir("story_1")) ?>,
           story_2: <?php count(dir("story_2")) ?>,
           story_3:<?php count(dir("story_3")) ?>
           }

But need to filter for JPG and return a number. What's the right code?

+5  A: 

If you want to count the number of jpg images in a directory you can do:

count(glob("dir/*.jpg"));

The glob function returns an array containing the matched files and then we use count on that array.

codaddict
That's pretty cool. I've never heard of that function before, so I made my own style show below, but I would think that your answer is much more concise. So +1 to you sir.
Mark Tomlin
`<?php count(glob("table/*.jpg")); ?>` returns nothing.`<?php echo count(glob("table/*.jpg")); ?>` returns 0, despite there being 19 images in the directory. What am I missing?
Isaac Lubow
Are you using the correct path, i.e. is the `table` directory in the same directory as your PHP script? Otherwise you could try an absolute path such as `$_SERVER['DOCUMENT_ROOT'] . '/graphics/table/*.jpg'`.
casablanca
Is the file extension really `.jpg` or maybe `.JPG` or `.Jpg`? Mind the case-sensitivity of your file system.
joschi
@Casablanca that did it! Thanks!
Isaac Lubow
+1  A: 

You could simply use the glob() function to retrieve all filenames ending in .jpg and count them.

If you want to make sure these are really JPEG files, you'll have to check them e. g. with finfo_file().

joschi
+1 for the checking the file is really what you want with finfo_file.
Mark Tomlin
A: 
function get_dir_structure($path, $recursive = TRUE, $ext = NULL)
{
    $return = NULL;
    if (!is_dir($path))
    {
        trigger_error('$path is not a directory!', E_USER_WARNING);
        return FALSE;
    }
    if ($handle = opendir($path))
    {
        while (FALSE !== ($item = readdir($handle)))
        {
            if ($item != '.' && $item != '..')
            {
                if (is_dir($path . $item))
                {
                    if ($recursive)
                    {
                        $return[$item] = get_dir_structure($path . $item . '/', $recursive, $ext);
                    }
                    else
                    {
                        $return[$item] = array();
                    }
                }
                else
                {
                    if ($ext != null && strrpos($item, $ext) !== FALSE)
                    {
                        $return[] = $item;
                    }
                }
            }
        }
        closedir($handle);
    }
    return $return;
}

Use that function to do this:

ar count = {
    table: <?php echo count(get_dir_structure("./graphics/table/", FALSE, '.jpg')) ?>,
    people: <?php echo count(get_dir_structure("./graphics/people/", FALSE, '.jpg')) ?>,
    places_details: <?php echo  count(get_dir_structure("./graphics/places_details/", FALSE, '.jpg')) ?>,
    story_1: <?php echo count(get_dir_structure("./graphics/story_1/", FALSE, '.jpg')) ?>,
    story_2: <?php echo count(get_dir_structure("./graphics/story_2/", FALSE, '.jpg')) ?>,
    story_3: <?php echo count(get_dir_structure("./graphics/story_3/", FALSE, '.jpg')) ?>
}
Mark Tomlin