使用PHP计算特定子目录中的图像

时间:2012-08-02 17:09:32

标签: php

我的图像存储在目录结构中有点像这样:

  • 图像
    • FOO
      • 拇指
        • awefawef.jpg
        • dtyhtyh.jpg
        • awefawef.jpg
        • dtyhtyh.jpg
        • eriguherg.jpg
      • 拇指
        • erg.jpg
        • erg.jpg
        • tyjhr5g.jpg
    • some_other_dir
    • 等...

(注意:“拇指”目录中的图像数量与其相邻的“完整”图像不会相同。)

所以:我需要使用PHP来计算所有“完整”目录中的图像总数。

我确信我可以通过获取“图像”的所有子目录列表,然后在它们中循环并计算每个“完整”文件夹中的图像来找到一种方法。但是由于每个文件夹中有数千个目录,数百个图像,我正在寻找最有效的解决方案 - 这需要经常由网站完成。

这里最有效的方法是什么?

3 个答案:

答案 0 :(得分:10)

这适用于您概述的情况:

$imagecount = count(glob("images/*/full/*.jpg"));

答案 1 :(得分:1)

$count = count(glob("images/*.jpg"));

echo "In images have $count images";

答案 2 :(得分:0)

试试这个: -

<?php

$directory='images';
$count=0;
$full_images='';

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

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

    if(is_dir($directory.'/'.$file))
    {
    if($handle1 = opendir($directory.'/'.$file."/full")) {

      while (false !== ($file1 = readdir($handle1))) {
        if(!is_dir($file1))
         {
            $count=$count+1;
        $full_images[$file]['full'][]=$file1;
          } 
      }
    }
    }
  }

closedir($handle);
}

echo "Total count images in all of the full directories in: <b>".$count."</b> <br />";

foreach($full_images as  $k => $v)
{
  foreach($v as $key)
  {
    echo "Total count images in ".$k."\'s full directory in:  <b>".count($key)."</b><br />";
  }

}
?>
相关问题