PHP: a good way to universalize paths across OSs (slash directions)
Solution 1
I'm aware of DIRECTORY_SEPARATOR,
However: 1. It's long to write
Laziness is never a reason for anything
$path = (DIRECTORY_SEPARATOR === '\\')
? str_replace('/', '\\', $subject)
: str_replace('\\', '/', $subject);
or
$path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
This will in one step replace "the right one" with itself, but that doesnt make any difference.
If you know for sure, that a path exists, you can use realpath()
$path = realpath($path);
However, that is not required at all, because every OS understands the forward slash /
as a valid directory separator (even windows).
Solution 2
You are missing the DIRECTORY_SEPARATOR
predefined constant.
Solution 3
If you're going to pass those paths to standard PHP functions, you actually don't need to fix paths, as far as I can tell. Basic functions like file_get_contents
or fopen
work perfectly fine with any kind of path you throw at them.
Related videos on Youtube
shealtiel
Updated on June 04, 2022Comments
-
shealtiel almost 2 years
My simple concern is being able to handle paths across OSs, mainly in the regard of back and forward slashes for directory separators.
I was using
DIRECTORY_SEPARATOR
, however:It's long to write
Paths may come from different sources, not necessarily controlled by you
I'm currently using:
function pth($path) { $runningOnWindows = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'); $slash = $runningOnWindows ? '\\' : '/'; $wrongSlash = $runningOnWindows ? '/' : '\\' ; return (str_replace($wrongSlash, $slash, $path)); }
Just want to know that there is nothing existing in the language that I'm reinventing,
Is there already an inbuilt PHP functon to do this?
-
Pekka about 13 yearsWell, it's not enough for what he wants to do (Build a generic method to convert any path to the current OS's format). But it's a start
-
Waihon Yew about 13 years@Pekka: Sure. But he can lose the custom OS detection, which IMHO is the most unpleasant thing in that code.
-
Charles about 13 yearsHell, you can even mix slashes on Windows.
'C:\Foo\Bar/baz/what.php'
is totally a legal path in PHP. -
KingCrunch about 13 yearsHehe, yeah, there is really no reason to think about that. As far as I know there is exactly one exception, where it makes a difference on win php.net/domdocument.load Also the solution is mentioned there ;)
-
shealtiel about 13 years"you can even mix slashes on Windows". Here is one example when this didn't work on windows: exec("rd path\with/mixed/slashes") regarded the / as a key for the command
-
KingCrunch about 13 yearsI dont know every pitfall on windows, but this topic is about a solution in php and a command executed via
exec()
has not that much to do with php. -
Pacerier almost 9 years@KingCrunch, You quoted him on "Laziness is never a reason for anything", but he definitely stated "Paths may come from different sources, not necessarily controlled by you" as the reason.
-
KingCrunch almost 9 years@Pacerier No, that was on "[DIRECTOR_SEPERATOR is] long to write". Sure, different sources can be a problem, but read the entire answer. The easiest solution remains to use
/
always, because it is accepted by every OS. (There is one exception; something XMLish ... ) -
Jason over 5 yearsLinux is happy to see a trailing
/
when accessing a directory, but I'm not sure Windows would like that at all. -
TonyG over 2 years
fx_slsh($path)
? Are vowels too expensive in this economy?fix_slash($path)