How can I define the DIRECTORY_SEPARATOR for both Windows and Linux platforms?

PHP accepts both \ and / as valid path separators in all OS. So just use / in your code


PHP understands '\' and '/' as path separators, regardless on the system you're on. I prefer to use '/' (the unix way) in all of my code. When you're on a windows box and there is a need to provide a full qualified windows/DOS path I'll have this simple, non destructive function

function dosPath($path){
    return str_replace('/', '\\', $path);
}

Example:

$drive = 'C:';
$path = '/tmp/uploads';

echo dosPath($drive.$path);
echo dosPath($path);

Please see PHP Predefined Constants

Maybe it's already defined in your script, try echoing DIRECTORY_SEPARATOR, see if it has any value


For convenience you can write define a shorter constant:

DEFINE('DS', DIRECTORY_SEPARATOR); 

and then write your path as:

$path = 'www'.DS.'app'.DS.'index'; 

Or do I not understand your question?

Tags:

Windows

Linux

Php