如何在 PHP 中获得独立于平台的目录分隔符?
我正在用 PHP 构建一个路径字符串.我需要它跨平台(即 Linux、Windows、OS X)工作.我正在这样做:
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;
假设 $someDirectory
和 $someFile
在各种平台上的运行时格式正确.这在 Linux 和 OS X 上工作得很好,但在 Windows 上不工作.问题是 /
字符,我认为它适用于 Windows.
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.
是否有 PHP 函数或其他一些技巧可以在 Windows 运行时将其切换到 ?
Is there a PHP function or some other trick to switch this to at runtime on Windows?
为了清楚起见,结果字符串是
Just to be clear, the resultant string is
c:Program Files (x86)SitefusionSitefusion.orgDefaultspref/user.preferences
在 Windows 上.显然,斜线的混合混淆了 Windows.
on Windows. Obviously the mix of slashes confuses Windows.
推荐答案
试试这个
DIRECTORY_SEPARATOR
DIRECTORY_SEPARATOR
$patch = $somePath. DIRECTORY_SEPARATOR .$someFile
或者你可以定义你的
PHP_OS == "Windows" ||
PHP_OS == "WINNT" ? define("SEPARATOR", "\") : define("SEPARATOR", "/");
相关文章