【问题标题】:php include path by using .. (double dot) to reference [closed]php通过使用..(双点)来引用包含路径[关闭]
【发布时间】: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


【解决方案1】:

不,这不是一个错误,它是窗口文件系统和 linux 文件系统之间的差异

【讨论】:

    猜你喜欢
    • 2016-03-22
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 2011-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多