Home > Archive > PHP Programming > February 2008 > Comparison function for directories?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Comparison function for directories?
|
|
| laredotornado@zipmail.com 2008-02-28, 10:05 pm |
| Hi,
I'm using php 4.4.4. Given two variables,
$dir1 = "/usr/local/apache2/htdocs/"
$dir2 = "/usr/local/apache2/htdocs"
What is a comparison function I could write that would say these two
directories are the same? A straight string comparison would not work
above.
Thanks for any insights, - Dave
| |
| Rik Wasmus 2008-02-28, 10:05 pm |
| On Thu, 28 Feb 2008 18:57:11 +0100, laredotornado@zipmail.com =
<laredotornado@zipmail.com> wrote:
> Hi,
>
> I'm using php 4.4.4. Given two variables,
>
> $dir1 =3D "/usr/local/apache2/htdocs/"
> $dir2 =3D "/usr/local/apache2/htdocs"
>
> What is a comparison function I could write that would say these two
> directories are the same? A straight string comparison would not work=
> above.
$is_the_same_dir =3D realpath($dir1) =3D=3D realpath($dir2);
-- =
Rik Wasmus
| |
| Álvaro G. Vicario 2008-02-29, 7:04 pm |
| *** Rik Wasmus escribió/wrote (Thu, 28 Feb 2008 19:00:09 +0100):
> $is_the_same_dir = realpath($dir1) == realpath($dir2);
Just two remarks:
- In Unix, the file system is case sensible. So (depending on what you need
this for) you may use === instead.
- realpath() returns FALSE when the path does not exist; take it into
account.
--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor austrohúngaro: http://www.demogracia.com
--
| |
| Toby A Inkster 2008-02-29, 7:04 pm |
| Ãlvaro G. Vicario wrote:
> Rik Wasmus escribió/wrote:
>
> - 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
[G 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/
|
|
|
|
|