Saturday, 31 August 2013

RecusriveDirectoryIterator - Getting files within folder with specific params

RecusriveDirectoryIterator - Getting files within folder with specific params

My end-goal is to grab only jpg|png files from within a directory (that
has directories in it). I first started with this article and settled on
the OOP approach (since this seems to be the best way forward). Of course,
with this comes some complexities that I haven't found a good example for
thusfar. The following example (taken from the PHP docs page) seems to get
me part of the way there, but I haven't found a good way to match all of
the requirements (below the example):
$ritit = new RecursiveIteratorIterator(new
RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST );
$r = array();
foreach ($ritit as $splFileInfo) {
$path = $splFileInfo->isDir()
? array($splFileInfo->getFilename() => array())
: array($splFileInfo->getFilename());
for ($depth = $ritit->getDepth() - 1; $depth >= 0; $depth--) {
$path =
array($ritit->getSubIterator($depth)->current()->getFilename() =>
$path);
}
$r = array_merge_recursive($r, $path);
}
print_r($r);
What I'd like to do is:
Specify a $directory (let's say /media/galleries)
Have it only look at directories first (so if any files are in that
top-level directory it ignores them)
Check if each directory is readable (it may do this by default already)
List out each jpg|png file within these files (again, making sure each is
readable which I believe it may do by default)
Ignore any/all dot files in the FilesystemIterator seems to do, but I'm
still getting the dreaded .DS_Store files in the top-level)
Store these as a multi-dimensional array
The aforementioned example seems to be almost there, but any help to steer
me in the right direction would be greatly appreciated. Thanks!

No comments:

Post a Comment