Re: Comparison function for directories?



Álvaro G. Vicario wrote:
Rik Wasmus escribió/wrote:

$is_the_same_dir = realpath($dir1) == realpath($dir2);

- In Unix, the file system is case sensible. So (depending on what you
need this for) you may use === instead.

The '==' operator is sufficient, as it is case-sensitive. The '===' will
also work, but is not required.

- realpath() returns FALSE when the path does not exist; take it into
account.

This may indeed cause problems, depending on exactly what the OP is hoping
to compare -- the directory paths of real, existing directories, or
strings representing directories which may or may not exist.

A possibility would be to replace realpath above with normalise_dirname:

function normalise_dirname ($dir)
{
if (realpath($dir))
return realpath($dir);
if (preg_match('#/$#', $dir))
return $dir;
return "$dir/";
}

If you needed to, you could further enhance it to deal with path
components like '.' and '..'.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 30 days, 22:28.]

Bottled Water
http://tobyinkster.co.uk/blog/2008/02/18/bottled-water/
.



Relevant Pages

  • Re: Comparison function for directories?
    ... In Unix, the file system is case sensible. ... So (depending on what you need ... realpath() returns FALSE when the path does not exist; ...
    (comp.lang.php)
  • Re: A Symbol- Abstracted Al
    ... depending at last. ... The unix of the compatible lounge is the ... breakfast that guarantees technically. ...
    (sci.crypt)
  • Re: user ip
    ... >> Depending on what version of unix you're running you can probably get ... >> it from the who command. ... On Solaris, ...
    (comp.unix.shell)
  • Re: Help - Writing Web Control to Access the Local FileSystem
    ... I'm not a web-OCX guru, but I suggest that this could cause huge ... security issues, allowing (depending on the features exposed) random ... phrase "File system access is the big one"). ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Windows Installer issue
    ... "Rob" wrote: ... > depending on the file system, there may be tools available to help him ... > restore the file tables. ...
    (microsoft.public.windowsxp.setup_deployment)