Comparison function for directories?

This is a discussion on Comparison function for directories? within the PHP Language forums, part of the PHP Programming Forums category; Hi, I'm using php 4.4.4. Given two variables, $dir1 = "/usr/local/apache2/htdocs/" $dir2 = "/...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008
laredotornado@zipmail.com
 
Posts: n/a
Default Comparison function for directories?

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
Reply With Quote
  #2 (permalink)  
Old 02-28-2008
Rik Wasmus
 
Posts: n/a
Default Re: Comparison function for directories?

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 = "/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.


$is_the_same_dir = realpath($dir1) == realpath($dir2);
--
Rik Wasmus
Reply With Quote
  #3 (permalink)  
Old 02-29-2008
Álvaro G. Vicario
 
Posts: n/a
Default Re: Comparison function for directories?

*** Rik Wasmus escribió/wrote (Thu, 28 Feb 2008 19:00:09 +0100):
>> $dir1 = "/usr/local/apache2/htdocs/"
>> $dir2 = "/usr/local/apache2/htdocs"
>>

> $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
--
Reply With Quote
  #4 (permalink)  
Old 02-29-2008
Toby A Inkster
 
Posts: n/a
Default 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/
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 01:32 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0