How to get a platform independent directory separator in PHP?(如何在 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", "/");
这篇关于如何在 PHP 中获得独立于平台的目录分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 PHP 中获得独立于平台的目录分隔符?


- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01