[英]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.