简体   繁体   English

列出所有目录,子目录并分别计算其中的所有图像

[英]List all directories, sub-directories and count all images within them separately

My folder structure only contains directories, sub-directories and images as either .JPG or .PNG . 我的文件夹结构仅包含目录,子目录和图像( .JPG.PNG I need to list all directories starting from the folder SOTT_photos/ and count images found in each sub-directory. 我需要列出从文件夹SOTT_photos/开始的所有目录,并计算在每个子目录中找到的图像。 I've added an example below: 我在下面添加了一个示例:

This is what the directory looks like: 这是目录的样子:

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

What I require it to show as: 我要求它显示为:

SOTT_photos 
    Plymouth
       2016
         010416
           berk
             3
           cras
             2
           jest
             1

          020414
            stan
              2
            bech 
              1

Here is the VERY BASIC, UNFINISHED code I am using, where I have run in to a problem with the counting images. 这是我正在使用的非常基本的未完成代码,在这里我遇到了计数图像的问题。

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');

This is what it is currently displaying: 这是当前显示的内容:

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

Which obviously isn't doing the correct thing. 这显然没有做正确的事。 Does anybody have any idea where I may be going wrong here? 有人知道我在这里哪里出错了吗?

You might be able to utilise the following perhaps. 您也许可以利用以下内容。 Edit the path $dir to suit your environment - I chose a random directory containing images on my development system to test the code, it seems to work ok. 编辑路径$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>';

}

** Edit ** The above does require a class called DirFileFilter which I now notice I did not include so do so now: **编辑**上面的确需要一个名为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();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM