PHP 需要“./”的问题
我在根目录中有一个文件index.php:
<?php
require_once "./include/common.php";
?>
在包含文件夹中有一个文件common.php:
<?php
require_once "globalConfig.php";
?>
文件globalConfig与common.php在同一文件夹中。树形文件夹为:
xxx/index.php
xxx/include/common.php
xxx/include/globalConfig.php
index.php 运行正常。但是,如果我将文件 common.php 更改为:
<?php
require_once "./globalConfig.php";
?>
PHP 将显示一条警告,指出它找不到 globalConfig.php 文件。有什么区别?我认为在“./”的情况下,最外面的包含文件(index.php)会在其当前目录中找到globalConfig.php。
I have a file index.php in root directory:
<?php
require_once "./include/common.php";
?>
and file common.php in include folder:
<?php
require_once "globalConfig.php";
?>
file globalConfig in the same folder with common.php. The tree folder as:
xxx/index.php
xxx/include/common.php
xxx/include/globalConfig.php
index.php run normally. But if I change file common.php as:
<?php
require_once "./globalConfig.php";
?>
PHP show a warning that it cannot find the globalConfig.php file. What is the difference? I think int the case with "./", the most outside including file (index.php) will find the globalConfig.php in its current directory.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在你的index.php中添加
然后在common.php中包含像这样。
这将找到确切的路径并标准化斜杠,然后每当您使用
BASE_PATH
时始终包含htdocs的根目录,即index.php。In your index.php add
And then within common.php include like so.
This will find the exact path and standerize the slashes, then whenever you use
BASE_PATH
alsways include from the root of your htdocs, ie index.php.该路径是相对于当前工作目录的;在 Web 上下文 (php-cgi) 中,这是最初调用的脚本(直接启动而不是通过
include
启动的脚本)所在的目录。一个简单的解决方法:
The path is relative to the current working directory; in a web context (php-cgi), this is the directory where the initially invoked script (the one that gets started directly instead of through on
include
) resides.An easy workaround:
我相信路径(即“./”)是相对于 BASE 脚本的,而不是相对于它所包含的文件。因此,我在包含脚本时通常使用绝对路径。
I believe that the path (i.e. "./") is relative to the BASE script, not the file it's contained within. For this reason, I usually use absolute paths when including scripts.