View Full Version : Dealing with Linux and Windows paths

07-26-2007, 01:56 AM
As we all know, Linux and Windows use different file system path nomenclature. In the app I am trying to write, I need to do file level work (create dir's, upload and move files, etc...). How do you typically deal with the differences between the two file systems when you create the file system paths?

For example:
Linux: mkdir('/home/e-smith/app/new_dirname')
Windows: mkdir('d:\wamp\www\app\new_dirname')

How do you make you app easy to switch between Linux/Unix and Windows?

The only method I can see at this point is to define two constants and manually edit them upon install. Create a ROOT_PATH and a PATH_SEPARATOR and use these when constructing the necessary file paths.
Something like:

$path=$ROOT_PATH . $PATH_SEPARATOR . 'new_dirname';
Is there a better way?

07-26-2007, 02:34 AM
I would use the $_SERVER predefined constants.

07-26-2007, 02:35 AM
You could use $_SERVER['DOCUMENT_ROOT'] which is the directory that the current file is in.

07-26-2007, 03:26 AM
Windows accepts both the \ and / -

On Windows, both slash (/) and backslash (\) are used as directory separator character. In other environments, it is the forward slash (/).