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?
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?
Share Improve this question asked Sep 20, 2010 at 5:26 Isaac LubowIsaac Lubow 3,5735 gold badges40 silver badges57 bronze badges3 Answers
Reset to default 11If 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.
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().
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')) ?>
}