PHP 需要“./”的问题

发布于 2024-09-18 05:15:21 字数 594 浏览 6 评论 0原文

我在根目录中有一个文件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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

所谓喜欢 2024-09-25 05:15:21

在你的index.php中添加

define('BASE_PATH',str_replace('\\','/',dirname(__FILE__)));

然后在common.php中包含像这样。

require_once BASE_PATH . '/includes/globalConfig.php';

这将找到确切的路径并标准化斜杠,然后每当您使用BASE_PATH时始终包含htdocs的根目录,即index.php。

In your index.php add

define('BASE_PATH',str_replace('\\','/',dirname(__FILE__)));

And then within common.php include like so.

require_once BASE_PATH . '/includes/globalConfig.php';

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.

栀子花开つ 2024-09-25 05:15:21

该路径是相对于当前工作目录的;在 Web 上下文 (php-cgi) 中,这是最初调用的脚本(直接启动而不是通过 include 启动的脚本)所在的目录。
一个简单的解决方法:

require_once dirname(__FILE__) . '/foobar.inc.php';

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:

require_once dirname(__FILE__) . '/foobar.inc.php';
尝蛊 2024-09-25 05:15:21

我相信路径(即“./”)是相对于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文