DIRECTORY_SEPARATOR is Useless!
October 14th, 2008 by Aaron
The predefined PHP constant DIRECTORY_SEPARATOR is useless.
When evaluated, the constant is as follows:
*nix: /
win: \
So, to the casual observer, there seems to be a real need for this constant - especially with those who favor the php command getcwd(). However, for the most part, it is just wasteful - and potentially error prone to have around.
For the slashes, Windows will work with / (even though its constant return is \ - and the backslash is used normally throughout the filesystem). Windows will also work with c:\blah\blah/additionalblah/moreblah.php (note the mixed slashes, just in case you happen to mix a call to something like getcwd() and your hardset path with the forward slash). *nix will not (you cannot do /usr\bin/funstuff)
I also ran across a case one time where - through some strange fate of mangled programming, the DIRECTORY_SEPARATOR on windows encased in a string started escaping certain characters of the filename.
One final note - someone on the PHP.net manual page suggested this function - if you really MUST use getcwd() - and in the case you’re using ‘explode()’ to figure out something about the path:
1 | return gstr_replace('\\', '/', getcwd()); |
Tags: PHP
This entry was posted on Tuesday, October 14th, 2008 at 8:13 am and is filed under PHP. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.















October 14th, 2008 at 10:07 am
I was going to disagree, but I can’t. Its useless. Except one minor thing. Since Unix file systems allow \ in a file name (its not smart to do, but possible), the above gstr_replace wouldn’t work in that one case. But oh well. Its not worth it to support that one case.