如何在工作目录上方的 php 中设置包含函数的绝对路径?

发布于 2024-08-28 05:10:23 字数 1359 浏览 5 评论 0原文

我正在运行一个脚本,

/wp-content/themes/currenttheme/chat.php

我想在上面的 php 中包含另一个脚本,该脚本位于

/forum/chat/index.php

The index.php 包含它自己的文件

我已经尝试过

$root = $_SERVER['DOCUMENT_ROOT'];
include($root."/forum/chat/index.php");

,但出现此错误

警告: require(D:/My Dropbox/xampp/htdocs/lib/custom.php) [function.require]: 无法打开流: D:\My Dropbox\xampp\htdocs\ 中没有这样的文件或目录forum\chat\index.php 第 17 行

致命错误:require() [function.require]:无法打开所需的 'D:/My Dropbox/xampp/htdocs/lib/custom.php' (include_path='.;\My Dropbox\xampp\php\PEAR ') 在 D:\My Dropbox\xampp\htdocs\forum\chat\index.php 第 17 行

(index.php 还包含一些文件,但 /forum/chat 在路径中以某种方式被省略)

然后我尝试

$path   = getcwd();
$myfile = "/forum/chat/index.php";
include ($path.$myfile);

并得到这个错误:

警告: include(D:\My Dropbox\xampp\htdocs\forum/forum/chat/index.php) [function.include]: 无法打开流: D:\My Dropbox\ 中没有这样的文件或目录xampp\htdocs\wp-content\themes\currenttheme\chat.php 第 24 行

警告:include() [function.include]:无法打开 'D:\My Dropbox\xampp\htdocs\forum/forum/chat/index.php' 进行包含 (include_path='.;\My Dropbox\xampp \php\PEAR') 在 D:\My Dropbox\xampp\htdocs\wp-content\themes\currenttheme\chat.php 第 24 行

I am running a script from

/wp-content/themes/currenttheme/chat.php

I want to include in the above php another one located in

/forum/chat/index.php

The index.php includes its own files

I already tried

$root = $_SERVER['DOCUMENT_ROOT'];
include($root."/forum/chat/index.php");

but I get this error

Warning: require(D:/My Dropbox/xampp/htdocs/lib/custom.php) [function.require]: failed to open stream: No such file or directory in D:\My Dropbox\xampp\htdocs\forum\chat\index.php on line 17

Fatal error: require() [function.require]: Failed opening required 'D:/My Dropbox/xampp/htdocs/lib/custom.php' (include_path='.;\My Dropbox\xampp\php\PEAR') in D:\My Dropbox\xampp\htdocs\forum\chat\index.php on line 17

(the index.php also includes some files, but the /forum/chat is ommited somehow in the path)

then I tried

$path   = getcwd();
$myfile = "/forum/chat/index.php";
include ($path.$myfile);

and got this error:

Warning: include(D:\My Dropbox\xampp\htdocs\forum/forum/chat/index.php) [function.include]: failed to open stream: No such file or directory in D:\My Dropbox\xampp\htdocs\wp-content\themes\currenttheme\chat.php on line 24

Warning: include() [function.include]: Failed opening 'D:\My Dropbox\xampp\htdocs\forum/forum/chat/index.php' for inclusion (include_path='.;\My Dropbox\xampp\php\PEAR') in D:\My Dropbox\xampp\htdocs\wp-content\themes\currenttheme\chat.php on line 24

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

高冷爸爸 2024-09-04 05:10:23

index.php没有问题。它正在被包括在内。
错误消息显示有关 custom.php 文件

只需对 custom.php 使用相同的 $_SERVER['DOCUMENT_ROOT'] 技术,

您必须手动添加 /forum/chat 作为没有路径可以省略

There is no problem with index.php. It is being included.
The error message says about custom.php file

Just use the same $_SERVER['DOCUMENT_ROOT'] technique for the custom.php

you have to add /forum/chat manually as there is no path to be omitted

苍暮颜 2024-09-04 05:10:23

有问题的是:

include('../../../forum/chat/index.php');

您发布的代码失败的原因有很多种。

C.

Something wrong with:

include('../../../forum/chat/index.php');

?

There are all sorts of reasons why the code you've published will fail.

C.

你在看孤独的风景 2024-09-04 05:10:23

用这个。

require_once(ABSPATH.'forum/chat/index.php');

这里 ABSPATH = WordPress 物理根目录路径,末尾带有斜杠

use this.

require_once(ABSPATH.'forum/chat/index.php');

here ABSPATH = WordPress physical root directory path with trailing slash

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