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 中获得独立于平台的目录分隔符?
基础教程推荐
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何替换eregi() 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
