【问题标题】:List all directories, sub-directories and count all images within them separately列出所有目录、子目录并分别统计其中的所有图像
【发布时间】:2016-07-22 12:25:24
【问题描述】:

我的文件夹结构仅包含.JPG.PNG 形式的目录、子目录和图像。我需要列出从文件夹 SOTT_photos/ 开始的所有目录,并计算在每个子目录中找到的图像。我在下面添加了一个示例:

这是目录的样子:

SOTT_photos 
    Plymouth
       2016
         010416
           berk
             img1.jpg
             img2.jpg
             img3.jpg
           cras
             img1.jpg
             img2.jpg
           jest
             img1.jpg

          020414
            stan
              img1.jpg
              img2.jpg
            bech 
              img1.jpg

我要求它显示为:

SOTT_photos 
    Plymouth
       2016
         010416
           berk
             3
           cras
             2
           jest
             1

          020414
            stan
              2
            bech 
              1

这是我正在使用的非常基本的未完成代码,我在计数图像时遇到了问题。

function listFolderFiles($dir)
{
    echo '<ol>';
    foreach (new DirectoryIterator($dir) as $fileInfo) {
        $image_count = 0;
        if (!$fileInfo->isDot()) {
            $file_path = $fileInfo->getPath();
            $file_name = $fileInfo->getFilename();


            if (substr($file_name, -4) == '.JPG' || substr($file_name, -4) == '.jpg') {
                $image_count++;
            } else {
            echo '<li>';
            echo $file_path.' | '.$file_name.'<br>';


            if ($fileInfo->isDir()) {
                listFolderFiles($fileInfo->getPathname());
            }


            echo '</li>';
            }


        }
        echo '<li>'.$image_count.'</li>';
    }
    echo '</ol>';
}
listFolderFiles('../SOTT_photos');

这是它当前显示的内容:

0
../SOTT_photos | Plymouth
0
../SOTT_photos/Plymouth | even
0
0
../SOTT_photos/Plymouth/even | 060216
0
0
1
1
1
1
1
1

这显然没有做正确的事情。有谁知道我在这里可能出错的地方吗?

【问题讨论】:

    标签: php directory


    【解决方案1】:

    您也许可以利用以下内容。编辑路径 $dir 以适应您的环境 - 我在我的开发系统上选择了一个包含图像的随机目录来测试代码,它似乎工作正常。

    $dir=ROOT.'/images/gallery';
    
    if( realpath( $dir ) ){
    
        $results=array();
    
        $dirItr = new RecursiveDirectoryIterator( $dir );
        $filterItr = new DirFileFilter( $dirItr, array(), $dir, 'all' );
        $recItr = new RecursiveIteratorIterator( $filterItr, RecursiveIteratorIterator::SELF_FIRST );
    
    
        foreach( $recItr as $filepath => $info ){
            $key = realpath( $info->getPathName() );
            if( $info->isDir() ) {
                $files=glob( $key . '/{*.jpg,.png,*.JPG,*.PNG,*.JPEG,*.jpeg}', GLOB_BRACE );
                $results[ $key ]=count( $files );
            }
        }
    
        $dirItr = $filterItr = $recItr = null;
    
        array_filter($results);
        echo '<pre>',print_r($results,true),'</pre>';
    
    }
    

    ** 编辑 ** 上面确实需要一个名为 DirFileFilter 的类,我现在注意到我没有包含它,所以现在这样做:

    class DirFileFilter extends RecursiveFilterIterator{
    
        protected $exclude;
        protected $root;
        protected $mode;
    
        public function __construct( $iterator, $exclude=array(), $root, $mode='all' ){
            parent::__construct( $iterator );
            $this->exclude = $exclude;
            $this->root = $root;
            $this->mode = $mode;
        }
    
        public function accept(){
            if( !is_readable( $this->root ) ) return false;
            $folpath=rtrim( str_replace( $this->root, '', $this->getPathname() ), '\\' );
            $ext=strtolower( pathinfo( $this->getFilename(), PATHINFO_EXTENSION ) );
    
            switch( $this->mode ){
                case 'all': 
                    return !( in_array( $this->getFilename(), $this->exclude ) or in_array( $folpath, $this->exclude ) or in_array( $ext, $this->exclude ) );
                case 'files':
                    return ( $this->isFile() && ( !in_array( $this->getFilename(), $this->exclude ) or !in_array( $ext, $this->exclude ) ) );
                break;
                case 'dirs':
                case 'folders':
                    return ( $this->isDir() && !( in_array( $this->getFilename(), $this->exclude ) ) && !in_array( $folpath, $this->exclude ) );
                break;
                default:
                    echo 'config error: ' . $this->mode .' is not recognised';
                break;
            }
            return false;
        }
    
    
    
        public function getChildren(){
            try{
                return new self( $this->getInnerIterator()->getChildren(), $this->exclude, $this->root, $this->mode );
            } catch( Exception $e ){
                echo $e->getMessage();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-03-08
      • 1970-01-01
      • 2011-11-09
      • 2011-11-09
      • 1970-01-01
      • 2016-08-07
      • 1970-01-01
      • 2012-11-14
      相关资源
      最近更新 更多