only list dirs if they have jpgs inside

only list dirs if they have jpgs inside

am 01.08.2007 06:52:03 von pos

Hi there,
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. I tried getting a list of each dir's contents and doing
if count($dir) > 0
But something not working out for me.


// $path : path to browse
// $maxdepth : how deep to browse (-1=unlimited)
// $mode : "FULL"|"DIRS"|"FILES"
// $d : must not be defined
function searchdir( $path , $maxdepth = -1 , $mode = "FULL" , $d = 0 )
{
if ( substr ( $path , strlen ( $path ) - 1 ) != '/' )
{ $path .= '/' ; }

$dirlist = array () ;
if ( $mode != "FILES" )
{ $dirlist[] = $path ; }

if ( $handle = opendir ( $path ) ){
while ( false !== ( $file = readdir ( $handle ) ) ){
if ( $file != '.' && $file != '..' ){
$file = $path . $file ;

if ( ! is_dir ( $file ) ) {
if ( $mode != "DIRS" ){$dirlist[] = $file ;} //if ( $mode NOT "DIRS" )
}elseif ( ($d >=0) AND ($d < $maxdepth || $maxdepth < 0) ) {
$result = searchdir ( $file . '/' , $maxdepth , $mode , $d + 1 ) ;
$dirlist = array_merge ( $dirlist , $result ) ;
}//elsif
}//if ( $file != '.' && $file != '..' )
}// while reading
closedir ( $handle ) ;
}// if( $handle = opendir ( $path )
if ( $d == 0 ) { natcasesort ( $dirlist ) ; }
return ( $dirlist ) ;
}//func
?>

thanks

Re: only list dirs if they have jpgs inside

am 01.08.2007 08:18:24 von pos

On Tue, 31 Jul 2007 21:52:03 -0700, J. Frank Parnell wrote:

>
>Hi there,
>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. I tried getting a list of each dir's contents and doing
>if count($dir) > 0
>But something not working out for me.
>
> >
>// $path : path to browse
>// $maxdepth : how deep to browse (-1=unlimited)
>// $mode : "FULL"|"DIRS"|"FILES"
>// $d : must not be defined
>function searchdir( $path , $maxdepth = -1 , $mode = "FULL" , $d = 0 )
>{
> if ( substr ( $path , strlen ( $path ) - 1 ) != '/' )
> { $path .= '/' ; }
>
> $dirlist = array () ;
> if ( $mode != "FILES" )
> { $dirlist[] = $path ; }
>
> if ( $handle = opendir ( $path ) ){
> while ( false !== ( $file = readdir ( $handle ) ) ){
> if ( $file != '.' && $file != '..' ){
> $file = $path . $file ;
>
> if ( ! is_dir ( $file ) ) {
>if ( $mode != "DIRS" ){$dirlist[] = $file ;} //if ( $mode NOT "DIRS" )
>}elseif ( ($d >=0) AND ($d < $maxdepth || $maxdepth < 0) ) {
> $result = searchdir ( $file . '/' , $maxdepth , $mode , $d + 1 ) ;
> $dirlist = array_merge ( $dirlist , $result ) ;
> }//elsif
> }//if ( $file != '.' && $file != '..' )
> }// while reading
> closedir ( $handle ) ;
> }// if( $handle = opendir ( $path )
> if ( $d == 0 ) { natcasesort ( $dirlist ) ; }

//dirlist now has all dirs ,i think.Lets weed out those with no jpgs, shall we?
foreach ($dirlist as $eachdir){
$filesineach = GetDirList($eachdir, $allowedExtensions=array('jpg','JPG'));
if (count($filesineach) > 0){
$newlist[] = $eachdir;
}//if count >0
}//foreach
$dirlist = $newlist;

> return ( $dirlist ) ;
>}//func
>?>

i stuck this code in there, seems to work. Is there a Better Way?

Re: only list dirs if they have jpgs inside

am 01.08.2007 13:55:46 von 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...

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;
}
?>

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 41 days, 15:27.]

demiblog 0.2.2 Released
http://tobyinkster.co.uk/blog/2007/07/29/demiblog-0.2.2/

Re: only list dirs if they have jpgs inside

am 01.08.2007 19:36:53 von Michael Fesser

..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...
>
> >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

Re: only list dirs if they have jpgs inside

am 01.08.2007 21:31:22 von pos

On Wed, 1 Aug 2007 12:55:46 +0100, Toby A Inkster
wrote:

>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...
>
> >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;
>}
>?>

Since i have php4, i tried this (thanks anyway Michael Fesser).

Works great, thanks! Could you explain 'bit of a cheat'? Am I going to get
caught? :)

I'm using this on a dir with about 4500 dirs, about 3300 of which have jpgs(the
rest are mostly ancestors of the jpg dirs), according to this func (which
matches what i came up with using my getDirlistAndForeachLookingForJpgs().)
find_dirs_with_jpegs () seems faster, though i didnt scientifically test it.

Re: only list dirs if they have jpgs inside

am 01.08.2007 22:32:21 von Michael Fesser

..oO(J. Frank Parnell)

>Works great, thanks! Could you explain 'bit of a cheat'?

The script doesn't do the dirty work itself, but delegates it to the
standard *nix command line tool 'find'.

>Am I going to get
>caught? :)

Only if you're ever going to run the script on a Windows machine ...

Micha

Re: only list dirs if they have jpgs inside

am 01.08.2007 22:54:29 von Toby A Inkster

J. Frank Parnell wrote:

> Works great, thanks! Could you explain 'bit of a cheat'? Am I going to
> get caught? :)

It calls two external programs in the background: "find" and "sort". You
will get caught if you use a non-Linux web host, as it may not have these
installed. Even other Unix flavours may cause problems as it specifically
relies on the GNU version of "find". (Non-Linux machines usually have the
BSD version of "find".)

> find_dirs_with_jpegs () seems faster

It probably will be: particularly for larger directories. "find" and
"sort" are written in C by some very smart people who have optimised them
very well.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 42 days, 24 min.]

demiblog 0.2.2 Released
http://tobyinkster.co.uk/blog/2007/07/29/demiblog-0.2.2/

Re: only list dirs if they have jpgs inside

am 02.08.2007 10:55:18 von pos

On Wed, 1 Aug 2007 21:54:29 +0100, Toby A Inkster
wrote:

>J. Frank Parnell wrote:
>
>> Works great, thanks! Could you explain 'bit of a cheat'? Am I going to
>> get caught? :)
>
>It calls two external programs in the background: "find" and "sort". You
>will get caught if you use a non-Linux web host, as it may not have these
>installed.

hmmm, maybe thats why it suddenly quit working. The array is now empty. Maybe
the hosting saw i was using it and turned it off? Of course, I can ask
(indirectly, I'm not really in charge of it), but is that something a host might
do?

Re: only list dirs if they have jpgs inside

am 02.08.2007 19:26:48 von Toby A Inkster

J. Frank Parnell wrote:

> hmmm, maybe thats why it suddenly quit working. The array is now empty.
> Maybe the hosting saw i was using it and turned it off? Of course, I
> can ask (indirectly, I'm not really in charge of it), but is that
> something a host might do?

It's unlikely, but hosts have been known to do all sorts of nasty things.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 42 days, 21:06.]

Open Mobile Alliance DTD Oops!
http://tobyinkster.co.uk/blog/2007/08/02/xhtml-mobile-oops/