Comparing directories

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Comparing directories

Post by alex.barylski »

I've ran into this problem a few times in my life. Comparing directories.

There are of course two obvious ways this can be done:

1) compare a hash of two directory paths or as is without a hash.
2) split each folder into a array and iteratively compare each element with the other array

The latter has the benefit of usually being more accurate. For instance depending on the server and system sometimes you have a path (from either $_SERVER['DOCUMENT_ROOT'] or other) where you have a leading or trailing slash added and other times not, so when comparing directories using a hash or a direct string comparison, you need to take caution into ensuring those caveats are removed. Otherwise two identical paths (resolve to the same object) may very well not compare as equals in code.

For former has the appeal of being easy to implement, trivial as possible ($a == $b) - it doesn't get better than that.

Because of the problem I just explained, I have considered prior to comparison, performing some quick cleanup. Such as removing slashes from the path and leading or trailing whitespace. Can you think of any gotcha's with this technique?

The reason I ask is, I am implemening a quick reverse-recursive globbing function, but for security reasons I never want it to travel outide the docroot, for this I need a accurate (bug free implementation) of path comparison.

Any opinions, suggestions, etc?

Cheers :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

That would work, assuming it didn't just canonicalize paths but also standardized trailing slash, etc...but...

Sometimes paths are relative and depending on what they are relative to, realpath returns NULL I think if you pass it a path which doesn't not exist, at least under the context of the CWD or whatever realpath uses.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Considering you're talking about traversing that directory structure, knowing it exists or not is important, no?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

touche. I should have mentioned that. Under this context realpath() will work as I am traversing a known directory. However I wanted to reuse the code if possible in a later part of the program which will be comparing relative paths.

Meh. No biggie.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It handles relative paths just fine too...
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Relative to CWD I believe yup. But consider instances where the file doesn't exist yet or the path is relative to something other than CWD.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'm pretty sure someone has posted code that handles this idea...
Post Reply