【发布时间】:2013-01-31 07:49:57
【问题描述】:
我有一个关于 PHP 包含路径的问题,它在两个环境中的行为不同。
文件夹结构:
Windows
|-C:\wamp\www\cms\themes\child
|-C:\wamp\www\cms\themes\parent
Linux
|-/var/www/html/cms/themes/child
|-/var/www/html/cms/themes/parent
Linux 环境
var_dump(realpath('/')); // means /
var_dump(realpath('/../parent/scripts/import.php')); //boolean false
include('/../parent/scripts/import.php'); //it will not work
include('../parent/scripts/import.php'); //it will not work, except it will reference parent folder
Windows 环境
var_dump(realpath('/')); //C:\
var_dump(realpath('/../parent/scripts/import.php')); //boolean false
include('../parent/scripts/import.php'); //am thinking it will work at first, but it does not work in windows (feel weird)
include('/../parent/scripts/import.php'); //it work in windows (feel weird)
我知道最好的做法是,它适用于两个平台
include(realpath(dirname(__FILE__)).'/../parent/scripts/import.php');
但是我想知道这个,是 PHP 的 bug 还是什么原因造成的?
【问题讨论】:
-
顺便说一句,
realpath()仅适用于存在的文件;即它不是字符串操作。 -
我可以肯定地说
"/../parent/scripts/import.php"不适用于这两种环境;你打错字了吗? -
嗨,杰克,正如我所提到的,路径是正确的,我只是想了解更多 Windows 工作的原因(但不符合逻辑)。据我了解,../ 表示引用父文件夹结构。
-
是的,
../表示父级,但/../表示根文件夹的父级(即根文件夹本身)。 -
为什么首先需要
realpath?
标签: php linux windows include realpath