0

在我的 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 函数来实现这种兼容性?任何想法都会有所帮助。谢谢你。

4

1 回答 1

0

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

/*
 * 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/
于 2020-06-20T14:54:35.940 回答