可以说我有一个这样的文件夹结构:
C:\xampp\htdocs\MyProject
|-- Lib
| |-- dbconn.php
|-- SuperSite
| |-- index.php
| |-- misc
| |-- misc.php
index.php 有这样一行:
require("../Lib/dbconn.php");
如果您从 Web 服务器访问 index.php,这将起作用,但如果您从命令行运行 index.php,则 require() 将失败:
C:\>php C:\xampp\htdocs\MyProject\SuperSite\index.php
Warning: include(../Lib/dbconn.php): failed to open stream: No such file or directory in C:\xampp\htdocs\MyProject\SuperSite\index.php
但是,如果我在路径前添加“/”,它不会失败:
require("/../Lib/dbconn.php");
如果我需要这样的其他文件,它也不会失败:
require("misc/misc.php");
或者
require("/misc/misc.php");
为什么从命令行运行脚本时需要在以“..”开头的路径前面有“/”?
(如果从 Web 服务器访问(使用浏览器),前面带或不带“/”的路径都有效)