首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用PHP返回目录中的图像计数?

如何使用PHP返回目录中的图像计数?
EN

Stack Overflow用户
提问于 2010-09-20 05:26:40
回答 3查看 1.5K关注 0票数 2

我有这样一个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并返回一个数字。正确的密码是什么?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-09-20 05:28:42

如果要计算目录中的jpg图像数量,可以这样做:

代码语言:javascript
复制
count(glob("dir/*.jpg"));

格罗布函数返回一个包含匹配文件的数组,然后使用该数组。

票数 11
EN

Stack Overflow用户

发布于 2010-09-20 05:32:35

您只需使用盖布()函数检索以.jpg结尾的所有文件名并对其进行计数。

如果你想确保这些文件真的是JPEG文件,你就必须用档案()来检查它们。

票数 1
EN

Stack Overflow用户

发布于 2010-09-20 05:35:02

代码语言:javascript
复制
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;
}

使用该函数执行以下操作:

代码语言:javascript
复制
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')) ?>
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3748892

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档