Directory separators and cross platform compatibility
I was curious about the impact of the directory separator and how that affects cross platform compatibilitye ver since I started developing on my Vista machine.
I've noticed how different they are compared to the Linux variety. Here's an example:
File path on *nix:
File path on Windows:
As you can see the separator is a slash, either a "/" or "" depending on the OS. Why does this matter? Because if we're dealing with files on the server, and the path is going to be recorded so they can be displayed on the Internet, through a URI, than this is going to introduce compatibility issues if not handled correctly. Sigh..
I've seen open source programs handle this type of issue through concatenation. For example the code:
<?php //$path = 'C:pathtofile.php'; Windows $path = '/path/to/file.php'; ?>
Is written like:
<?php //$ds = ''; Windows $ds = '/'; $path = $ ds.'path'.$ds.'$to.'file.php'; ?>
Is this even an issue anymore? Both Firefox 2.0 and IE 7 render HTML assets whether the slash is one way or another. Maybe if all paths are written like the *nix variety it's all good...
I did notice that when you have a dual slash Windows chokes:
<?php $path = '//path/to//dir'; ?>
Just something to think about...
According to the php manual:
On Windows, both slash (/) and backslash (\) are used as directory separator character. In other environments, it is the forward slash (/).
With that in mind it seems kind of dumb to even worry about this anymore. Just use the *nix variety and you should be fine.