You are here: Re: only list dirs if they have jpgs inside « PHP Programming Language « IT news, forums, messages
Re: only list dirs if they have jpgs inside

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация