473,385 Members | 1,325 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
Feb 28 '08 #1
3 1360
On Thu, 28 Feb 2008 18:57:11 +0100, la***********@zipmail.com
<la***********@zipmail.comwrote:
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
Feb 28 '08 #2
*** 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
--
Feb 29 '08 #3
Ã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/
Feb 29 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bill W. | last post by:
I'm teaching myself VB6. As a first project, I've decided to work on a Window's version of the Unix command, "diff" -- with a few twists. But being unfamiliar with VB, I am unsure as to how to...
6
by: aurgathor | last post by:
Howdy, How do I pass some function a generic comparison function? I figured out one non-generic case, but since this code got parameter declarations in two places, it's obviously not generic....
37
by: spam.noam | last post by:
Hello, Guido has decided, in python-dev, that in Py3K the id-based order comparisons will be dropped. This means that, for example, "{} < " will raise a TypeError instead of the current...
14
by: Spoon | last post by:
Hello, I've come across the following comparison function used as the 4th parameter in qsort() int cmp(const void *px, const void *py) { const double *x = px, *y = py; return (*x > *y) -...
0
by: SvenMathijssen | last post by:
Hi, I've been wrestling with a problem for some time that ought to be fairly simple, but turns out to be very difficult for me to solve. Maybe someone here knows the answer. What I try to do is...
4
by: metalinc | last post by:
hi...im new to C programming...need help...tried to run this code but got this error fork.c: In function ‘parse’: fork.c:44: warning: comparison between pointer and integer fork.c:51: warning:...
1
by: Lars B | last post by:
Hey guys, I have written a C++ program that passes data from a file to an FPGA board and back again using software and DMA buffers. In my program I need to compare the size of a given file against...
13
by: ElderGeek | last post by:
Hello. I'm pretty new to C, and having a problem which I suspect this has to do with how I'm handling (or not handling) pointers. I have an array of dirent structures which I am passing to a...
4
by: rasmidas | last post by:
There are two different directories. sparse/src/stk/dmg_apiutil and sparse/src/stk/dmg_meta_doc Inside these two directories there are lot of C++ files. I have written a function:
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.