as a long time windows user and php developer, i can only say: do not use DIRECTORY_SEPARATOR
! it has no real benefit but really can break things.
use /
. it is the better choice nearly every time.
if you use DIRECTORY_SEPARATOR
, which resolves to \
on windows, things tend to break. For example:\
gets converted to %5C
in urls.
http://example.com/media/cache/resolve/my_thumb/d/0/1%5Cfilename.jpg
if DIRECTORY_SEPARATOR
is used, it easily happens that the (relative) path, which will contain a backslash on windows, is passed to something which should be a url. assuming that most of the php developers are using linux or mac (i personally don't know many people who develop php on windows), they don't notice that in a windows environment a backslash is passed to the url and gets escaped as %5C
which leads to urls like in the example above.
windows and php on windows are capable of opening files with the path foo/bar\example/test.txt
so DIRECTORY_SEPARATOR
is of no real use except for easily detecting windows or for example if you export a path for a file list where a windows user would be confused to see /
instead of \
λ cat ./foo/bar\example/test.txt
mycontent
(yes cat
exists on windows)
notepad ./foo/bar\example/test.txt
also opens the file
foo.php
:
<?php
$content = file_get_contents('foo/bar\example/test.txt');
var_dump($content);
λ php foo.php
C:\test\foo.php:5:
string(9) "mycontent"
even that works:
<?php
$content = file_get_contents('c:/foo/bar\example/test.txt');
var_dump($content);
- cat for windows is shipped with git for windows https://git-scm.com/download/win
- the
λ
comes from the shell i use. http://cmder.net/ - meme linked from https://imgflip.com/
- cover image by wezlo https://www.pexels.com/photo/caution-dead-end-post-safety-237189/ https://pixabay.com/en/sign-street-dead-end-1749093/
Top comments (9)
You are not supposed to use DIRECTORY_SEPARATOR on URLs. You should use it only for working with directories and files.
Agreed. My rule of thumb now that I've discovered this constant is to use it just for file directory access. The problems illustrated seem to only occur when dealing with URLs. For me it's an anal stylistic choice. Thank you for the warning though, Mr Author
linux user won't notice if their program will return
\
on windows. so they don't recognize if a relative path will be exposed to an url somewhere.You don't have to test it on Windows. You just have to do
str_replace($url, '\\', '//')
at every location in your code which deals with URLs. Backslashes, even encoded ones, are not part of normal common URLs, so this code may even help prevent possible bugs. If you have special needs that require an encoded backslash to be preserved, you can tweak this function. It's that simple.Furthermore, you really shouldn't be hosting a server on Windows. It's a terrible awful very bad idea. You never know when MS is going to push the next update that's going to crash your server or make the CIA's backdoors to your PC vulnerable or worse. Linux and BSD are where it's at, and any self-respecting dev uses a *nix server.
I understand URL's slashes came from UNIX based directory separator so DIRECTORY_SEPARATOR is almost equal to the URL's.
Though, I think there are a bunch of Windows PHP coders in the world, at least in Asia. (I'm a macOS user but I've seen many)
As a info, in a non-English locale env such as Japan, backslash \ is replaced as ¥ as a directory separator. Yes, different ASCII code they are, so even more annoying.
So if you aim to code for an international usage, or run the script on their env., I think DIRECTORY_SEPARATOR is worth to care about it.
Since, with this, you can think less about the locale to access files, run the script. Except if you're using it for URL or directory path.
and to which symbol
/
is replaced? if it stays the same your argument that\
is replaced to¥
also hardens my argument to only use/
as separator/
(as a directory separator) is¥
on Japanese Win env.And on non-UNICODE encoding env such as Shift-JIS,
\
is displayed also as¥
.Since we have to handle a lot of encodings (Shift-JIS, JIS, EUC, UTF-8, UTF-16) until all the systems become UTF-16 or at least UTF-8, we have to convert encoding each other.
The problem is, Windows10 JP uses half Shift-JIS and half UTF-16 inside, so you can type
/
,¥
,\
as is.But once changing the encoding (UTF-8 -> S-JIS) it becomes a mess. Imagine
\
as escaping and directory as/
becomes all¥
.Well, I agree "always using
/
as a directory separator" would be nice. Really.But so far I think for directory separator purposes using
DIRECTORY_SEPARATOR
is safer.that sounds odd... of course a thing to keep in mind.
not sure if you can generalize that. as i wrote in my post, people are using the
DIRECTORY_SEPARATOR
and it happened to me more than once, that the well-intentionedDIRECTORY_SEPARATOR
simply broke stuff.of course you arguments are really good ones, i simply try to balance what happens more often, a special encoding which is known to break things or a
DIRECTORY_SEPARATOR
which is used wrong because it can't be tested on linux systems.But you shouldn't use DIRECTORY_SEPARATOR on urls, hence your "simply broke stuff" issue.
It's like using
move_uploaded_file
to move file A.txt to B.txt, it simply won't work.