2016-07-22 93 views
0

我的文件夾結構只包含.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 

這顯然不是做正確的事情。有人知道我在哪裏可能會出錯嗎?

回答

0

你也許能夠利用以下內容。編輯路徑$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(); 
     } 
    } 
} 
相關問題