Julian
Posted on May 21, 2018
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/
Posted on May 21, 2018
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.