Made of Everything You're Not

If you're a stalker I'd prefer if you didn't kill me. Thanks.
  • Home
  • Projects
  • Portfolio
  • Resume
« Mailchimp vs Emma
How to Kill a Project in 1 Decision »

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:

/path/to/file

File path on Windows:

C:\\path\to\file
Dazed and Confused

Dazed and Confused

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…

UPDATE::
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.

Bookmark and Share

Related Posts

Stand Alone ExpressionEngine Authentication
Importing Legacy Users Into ExpressionEngine
CartThrob 2.0 Beta Fun
ExpressionEngine and the Mystery of M00o93H7pQ09L8X1t49cHY01Z5j4TT91fGfr
Custom Routes With Zend Framework

Tags: Linux, php, Windows

This entry was written by Eric Lamb and posted on Friday, February 20th, 2009 at 12:00 pm and is filed under Code, IT, Programming. 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.

3 Comments

  1. eddy says:
    May 19, 2009 at 6:10 pm

    PHP has a DIRECTORY_SEPARATOR constant for that. And you can define a shorter constant by:
    DEFINE(‘DS’, DIRECTORY_SEPARATOR);

    Reply
  2. Eric Lamb says:
    May 19, 2009 at 10:13 pm

    Yeah, but it’s still kind of a pain to have to even setup your pathing like:


    DS.'path'.DS.'to'.DS.'file'

    Considering Windows accepts both forward and backward slashes I’m opting to not even consider this an issue and just use *nix pathing for both platforms.

    '/path/to/file'

    Sloppy? Maybe. But I have too much to worry about without path separators.

    Reply
  3. Kai Sellgren says:
    October 24, 2010 at 3:22 am

    This is probably one of the most misunderstood features in PHP. You don’t use DIRECTORY_SEPARATOR to create paths — you use it to parse/explode/etc. paths given by PHP (the document root, php ini path, etc.). If you want to create a path, just use the forward slash.

    Reply

Leave a Reply

Click here to cancel reply.

  • Subscribe: Entries | Comments
  • About Me

    Email Email
    Twitter Twitter
    310.739.3322
  • Categories

    • Brain Dump
    • Business
    • Code
    • IT
    • Programming
    • Rant
    • Servers
  • Archives

    • October 2011
    • August 2011
    • July 2011
    • June 2011
    • May 2011
    • April 2011
    • March 2011
    • February 2011
    • January 2011
    • December 2010
    • November 2010
    • October 2010
    • September 2010
    • August 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008

Copyright © 2008 - 2012 Eric Lamb - All rights reserved