Click here to get back home

something like grep

 HomeNewsGroups | Search | About
 comp.lang.php    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
something like grep Jeff 07-29-2008
Get Chitika Premium
Posted by Jeff on July 29, 2008, 4:47 pm
Please log in for more thread options
I'd like to open a directory and spider all the subdirectories and files.

What is the php term for doing this?

Seems like it should be here:

http://us3.php.net/manual/en/ref.filesystem.php

Perhaps I'm just having a bad day!

Jeff

Posted by petersprc on July 29, 2008, 5:01 pm
Please log in for more thread options
Hi,

You can use the opendir and readdir functions to traverse a directory
tree. There's an example in the doc at php.net/opendir.

If you want to do a text search within a directory tree, you can use
the egrep shell command with the -r (recursive) option on most
linuxes.

Regards,

John Peters

> I'd like to open a directory and spider all the subdirectories and files.
>
> What is the php term for doing this?
>
> Seems like it should be here:
>
> http://us3.php.net/manual/en/ref.filesystem.php
>
> Perhaps I'm just having a bad day!
>
> Jeff


Posted by Michael Fesser on July 29, 2008, 7:16 pm
Please log in for more thread options
.oO(petersprc)

>You can use the opendir and readdir functions to traverse a directory
>tree. There's an example in the doc at php.net/opendir.

Or use the various iterators of the SPL. They make it quite convenient
to loop through a directory and all that's beneath it if necessary.

Micha

Posted by Jeff on July 29, 2008, 7:54 pm
Please log in for more thread options
Michael Fesser wrote:
> .oO(petersprc)
>
>> You can use the opendir and readdir functions to traverse a directory
>> tree. There's an example in the doc at php.net/opendir.
>
> Or use the various iterators of the SPL. They make it quite convenient
> to loop through a directory and all that's beneath it if necessary.

Having just now looked at this, I can see how this might work (It
seems to have some of the file system operators I'm familiar with). But
the documentation is poor and I'm not sure where I would even start.

Do you have any sample code for this?

Recursion and readdir is it's own can of worms, but at least I
understand it!

Jeff
>
> Micha

Posted by Michael Fesser on July 29, 2008, 9:15 pm
Please log in for more thread options
.oO(Jeff)

>Michael Fesser wrote:
>> .oO(petersprc)
>>
>>> You can use the opendir and readdir functions to traverse a directory
>>> tree. There's an example in the doc at php.net/opendir.
>>
>> Or use the various iterators of the SPL. They make it quite convenient
>> to loop through a directory and all that's beneath it if necessary.
>
> Having just now looked at this, I can see how this might work (It
>seems to have some of the file system operators I'm familiar with). But
>the documentation is poor and I'm not sure where I would even start.
>
> Do you have any sample code for this?

To conveniently loop through an entire directory tree $path you would
need two iterators, like this:

$path = '/foo/bar/whatever';
$di = new RecursiveDirectoryIterator($path);
foreach (new RecursiveIteratorIterator($di) as $item) {
if ($item->isFile()) {
...
}
}

Of course this doesn't pay attention to the actual directory or nesting
level, it simply loops through _all_ found entries and sub entries.

See <http://www.php.net/~helly/php/ext/spl/classSplFileInfo.html> for
the various methods you can call on such file objects (just scroll down
a bit to "Public Member Functions").

Micha

Similar ThreadsPosted
grep in php March 31, 2005, 3:33 pm
[Q] Need help with grep expression January 31, 2005, 2:31 am
listing directory with "grep" June 10, 2005, 6:52 am
simple grep question May 24, 2007, 6:26 pm
Help in search linux command grep need urgently June 27, 2005, 3:58 am
'grep' a file - output to browser not working October 31, 2007, 7:45 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap