PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > Comparison function for directories?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Comparison function for directories?

Réponse
 
LinkBack Outils de la discussion
Vieux 28/02/2008, 17h57   #1
laredotornado@zipmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 28/02/2008, 18h00   #2
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 29/02/2008, 15h35   #3
Álvaro G. Vicario
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
--
  Réponse avec citation
Vieux 29/02/2008, 16h18   #4
Toby A Inkster
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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/
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 20h44.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10551 seconds with 12 queries