Cross Platform Directory Slashes In PHP

I'm not sure where I found this, but I have been using it on a few projects recently and it's helped a lot. It basically detects what system you are on and will give you a constant that keeps hold of the slash for that system.

if (strtoupper(substr(PHP_OS,0,3)) == 'WIN') {
 // Windows
 define('SLASH', '\\');
} else {
 // Linux/Unix 
 define('SLASH', '/');
}

For example, on a Windows system a file might be in C:\folders\data\, whereas on Linux the file would be in /folders/data/. So if you are given the full path as a string it can be difficult to separate the filename from the directory without knowing what system you are on.

Comments

Isn't that what DIRECTORY_SEPARATOR does?
Permalink

This is pointless. Windows works just fine using forward slashes like Linux.

Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
8 + 7 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.