如何在 PHP 中获得独立于平台的目录分隔符? [英] How to get a platform independent directory separator in PHP?

查看:13
本文介绍了如何在 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 中获得独立于平台的目录分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆