简体   繁体   中英

Slashes and backslashes uniform compatibility in PHP for UNIX and Windows

In my PHP code, I have this:

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

When I execute my program on a Windows server, I get this:

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

When I execute my program on a UNIX server, I get this:

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

My program is working correctly on the Windows server, but it is not working properly on the UNIX server. Should I first detect the operating system and after that, use the corresponding code? Is there a built-in PHP function to achieve this type of compatibility? Any ideas will be helpful. Thank you.

I found the following somewhere in my code:

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

I guess that DIRECTORY_SEPARATOR is a constant built-in for CakePHP. I am not sure. But it works. I used this:

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

Now it works on both UNIX and Windows. For Windows I get this:

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

For UNIX I get this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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