74

I'm building a path string in PHP. I need it to work across platforms (i.e., Linux, Windows, OS X). I'm doing this:

$path = $someDirectory.'/'.$someFile;

Assume $someDirectory and $someFile are formatted correctly at run-time on the various platforms. This works beautifully on Linux and OS X, but not on Windows. The issue is the / character, which I thought would work for Windows.

Is there a PHP function or some other trick to switch this to \ at runtime on Windows?

EDIT: Just to be clear, the resultant string is

c:\Program Files (x86)\Sitefusion\Sitefusion.org\Defaults\pref/user.preferences

on Windows. Obviously the mix of slashes confuses Windows.

icedwater
  • 4,701
  • 3
  • 35
  • 50
retrodrone
  • 5,850
  • 9
  • 39
  • 65
  • possible duplicate of [How do I properly split a PATH variable in PHP?](http://stackoverflow.com/questions/1259450/how-do-i-properly-split-a-path-variable-in-php) – AJ. Jul 11 '11 at 17:46
  • 4
    Worth to mention: Windows works fine when using `/` as directory separator. There is usually no need to make it platform dependent. I don't know, what you mean with "the mix [..] confuses Windows" – KingCrunch Jul 11 '11 at 17:49
  • Thanks, @AJ. I missed that question. – retrodrone Jul 11 '11 at 17:51

2 Answers2

134

Try this one

DIRECTORY_SEPARATOR

$patch = $somePath. DIRECTORY_SEPARATOR .$someFile

or you can define yours

PHP_OS == "Windows" ||
    PHP_OS == "WINNT" ? define("SEPARATOR", "\\") : define("SEPARATOR", "/"); 
icedwater
  • 4,701
  • 3
  • 35
  • 50
genesis
  • 50,477
  • 20
  • 96
  • 125
  • No joy, this gives me the same string on Windows. I tried PATH_SEPARATOR, too. – retrodrone Jul 11 '11 at 17:51
  • 12
    however, both, Windows and Linux supports / so what's problem ? – genesis Jul 11 '11 at 17:52
  • 2
    The problem in my instance is I'm getting the first half of the path ($someDirectory) dynamically (from XULRunner) which uses '\' on Windows. PHP is giving '/' and Windows barfs at the slash combination. – retrodrone Jul 11 '11 at 17:53
  • @retrodone: You should describe, what happens instead. What (in your case) does "barfs at the slash combination" mean? You may add it to your question. I'm starting to think, that the `/` isn't the real problem. – KingCrunch Jul 11 '11 at 17:56
  • @King: I think that he gets C:\path\to\his\dir/but/now/there/is/dynamic/dir and he don't want to mash it up – genesis Jul 11 '11 at 17:57
  • Why would `DIRECTORY_SEPARATOR` return `/` on his Windows box? – webbiedave Jul 11 '11 at 18:03
  • @KingCrunch, I get the following error from XULRunner: [Exception... "Component returned failure code: 0x80520001 (NS_ERROR_FILE_UNRECOGNIZED_PATH) [nsILocalFile.initWithPath]" nsresult: "0x80520001 (NS_ERROR_FILE_UNRECOGNIZED_PATH)" location: "JS frame :: chrome://sitefusion/content/sitefusion.js -> resource://sflibcache/class/fileHandling.js :: :: line 125" data: no] – retrodrone Jul 11 '11 at 19:04
  • It's the JavaScript generated by PHP. I'm using SiteFusion. – retrodrone Jul 11 '11 at 19:09
  • 1
    Instead of "Windows" my server resturn "WINNT". Just in case. – spikey Apr 17 '13 at 10:23
  • 3
    **Don't bother**, this IS NOT necessary. – doublejosh Jun 26 '13 at 01:31
  • 4
    Not that it's critically important, but `PATH_SEPARATOR` is something else entirely (`:` on *nix, `;` on Windows) and isn't a synonym of `DIRECTORY_SEPARATOR`. – icedwater May 05 '15 at 09:02
  • @retrodrone just parse your input then, split it by directory separator, and then reconstitute it using a single directory separator. – Rolf Jun 23 '15 at 02:14
8
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')           
    define("SEPARATOR", "\\");
else 
    define("SEPARATOR", "/");

http://php.net/manual/en/function.php-uname.php

Maxim Mikhisor
  • 342
  • 4
  • 7