繁体   English   中英

UNIX 和 Windows 的 PHP 中的斜杠和反斜杠统一兼容性

[英]Slashes and backslashes uniform compatibility in PHP for UNIX and Windows

在我的 PHP 代码中,我有这个:

$directory = WWWROOT."img\\".$component."\\ProductTemplate\\";

当我在 Windows 服务器上执行我的程序时,我得到这个:

C:\Users\jaimemontoya\[path]\app\webroot\img\medium_thumb\ProductTemplate\

当我在 UNIX 服务器上执行我的程序时,我得到这个:

/home/jaimemon/public_html/[path]/app/webroot/img\medium_thumb\ProductTemplate\

我的程序在 Windows 服务器上运行正常,但在 UNIX 服务器上运行不正常。 我应该先检测操作系统,然后再使用相应的代码吗? 是否有内置的 PHP function 来实现这种兼容性? 任何想法都会有所帮助。 谢谢你。

我在代码的某处发现了以下内容:

/*
 * Use the DS to separate the directories in other defines
 */
if (!defined('DS')) {
    define('DS', DIRECTORY_SEPARATOR);
}

我猜DIRECTORY_SEPARATOR是 CakePHP 的内置常量。 我不知道。 但它有效。 我用这个:

$directory = WWWROOT."img".DS.$component.DS."ProductTemplate".DS;

现在它适用于 UNIX 和 Windows。 对于 Windows 我得到这个:

C:\Users\jaimemontoya\[path]\app\webroot\img\medium_thumb\ProductTemplate\

对于 UNIX 我得到这个:

/home/jaimemon/public_html/[path]/app/webroot/img/medium_thumb/ProductTemplate/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM