|
Posted by Michael Fesser on 08/01/07 17:36
..oO(Toby A Inkster)
>J. Frank Parnell wrote:
>
>> I got this directory tree listing function somewhere, it works great,
>> but I want it to only list directories that do have one or more .jpgs
>> inside.
>
>It's a bit of a cheat, but...
>
><?php
>function find_dirs_with_jpegs ($path)
>{
> $path = addslashes($path);
> $find = "find '{$path}' -iregex '.*\\.jpe?g' -printf '%h\\n'";
> $dirs = explode("\n", `$find | sort -u`);
> array_pop($dirs);
> return $dirs;
>}
>?>
;-)
In PHP 5 it could be done in a "non-cheating way" with iterators for
example. The RecursiveDirectoryIterator, maybe in conjunction with a
RecursiveIteratorIterator (sic!), can be very helpful:
function findDirsWithJpegs($path) {
$result = array();
$di = new RecursiveDirectoryIterator($path);
foreach (new RecursiveIteratorIterator($di) as $item) {
if ($item->isFile() && preg_match('#\.jpe?g$#i', $item->getFilename())) {
$result[] = $item->getPath();
}
}
return $result;
}
Micha
Navigation:
[Reply to this message]
|