我有这样一个Javascript对象:
变量计数={表:19,人:39,places_details:84,story_1:18,story_2:6,story_3:11 }
每一项(桌子、人等)是位于站点根目录的Mygraphics/目录中的一个目录。我想使用PHP通过计算对应目录中的JPG图像来提供数字值。我想是这样的:
ar ={ table:,people:,places_details:,story_1:,story_2:,story_3:}
但是需要过滤JPG并返回一个数字。正确的密码是什么?
发布于 2010-09-20 05:28:42
如果要计算目录中的jpg图像数量,可以这样做:
count(glob("dir/*.jpg"));格罗布函数返回一个包含匹配文件的数组,然后使用该数组。
发布于 2010-09-20 05:32:35
您只需使用盖布()函数检索以.jpg结尾的所有文件名并对其进行计数。
如果你想确保这些文件真的是JPEG文件,你就必须用档案()来检查它们。
发布于 2010-09-20 05:35:02
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;
}使用该函数执行以下操作:
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')) ?>
}https://stackoverflow.com/questions/3748892
复制相似问题