如何在PHP中获得独立于平台的目录分隔符


How to get a platform independent directory separator in PHP?

我正在用PHP构建一个路径字符串。我需要它跨平台(如Linux、Windows、OS X(工作。我在做这个:

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

假设$someDirectory$someFile在各种平台上的运行时被正确格式化。这在Linux和OSX上运行得很好,但在Windows上则不然。问题是/字符,我认为它适用于Windows。

有没有PHP函数或其他技巧可以在Windows运行时将其切换到'

编辑:为了清楚起见,结果字符串是

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

在Windows上。很明显,斜杠的混合让Windows感到困惑。

试试这个

目录分隔符

$patch = $somePath. DIRECTORY_SEPARATOR .$someFile

或者你可以定义你的

PHP_OS == "Windows" ||
    PHP_OS == "WINNT" ? define("SEPARATOR", "''") : define("SEPARATOR", "/"); 
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')           
    define("SEPARATOR", "''");
else 
    define("SEPARATOR", "/");

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