我的文件夹结构仅包含.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 :(得分: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();
}
}
}