Page 1 of 1

Comparing directories

Posted: Sun Jun 17, 2007 8:21 pm
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 :)

Posted: Sun Jun 17, 2007 8:24 pm
by feyd

Posted: Sun Jun 17, 2007 8:26 pm
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.

Posted: Sun Jun 17, 2007 8:33 pm
by feyd
Considering you're talking about traversing that directory structure, knowing it exists or not is important, no?

Posted: Sun Jun 17, 2007 9:27 pm
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.

Posted: Sun Jun 17, 2007 9:55 pm
by feyd
It handles relative paths just fine too...

Posted: Sun Jun 17, 2007 11:18 pm
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.

Posted: Mon Jun 18, 2007 12:00 am
by feyd
I'm pretty sure someone has posted code that handles this idea...