views:

83

answers:

4

Hello,

I am using below statement to return the directory name of the running script:

print dirname(__FILE__);

it outputs something like this with back-slashes:

www\EZPHP\core\ezphp.php

Question:

Is a path with back-slashes acceptable across all major operating systems? If not, how should i construct the path either with slashes or back-slashes so that it is acceptable on all major operating systems eg Windows, Linux, Ubuntu.

Thank You.

+2  A: 

I would normalize that to forward slashes. Windows accepts forward slashes, and they are the default on *nix systems

print str_replace('\\','/',dirname(__FILE__));

Bryan Ross
@Ross: "and they are the default on *nix systems". So forward slashes are default on *nix systems but are back-slashes supported on them?
Sarfraz
+1  A: 

In reality, it doesn't matter... this is because dirname() doesn't necessarily return backslashes: it returns whatever directory separator is used by the OS. That is to say, whatever dirname returns is the separator you should be using anyway.

Other than that, just use forward slashes: PHP will interpret it correctly in Windows and Linux.

Narcissus
+2  A: 

Forward slashes are a good route.

There is also a constant called DIRECTORY_SEPARATOR that will return the directory separator for the system the code is running on.

I use forward slashes when I write paths for all my apps, and I often use DIRECTORY_SEPARATOR when I am exploding the results of a call that returns a path so that I can ensure I always have the right one to break on.

HTH, Jc

JC
+1  A: 

It doesn't matter, dirname() always return the path in the OS format.

dirname('c:/x'); // returns 'c:\'
dirname('c:/Temp/x'); // returns 'c:/Temp'
dirname('/x'); // returns '\'
Pentium10