如何在工作目录上方的 php 中设置包含函数的绝对路径?
我正在运行一个脚本,
/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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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 thecustom.php
you have to add /forum/chat manually as there is no path to be omitted
有问题的是:
?
您发布的代码失败的原因有很多种。
C.
Something wrong with:
?
There are all sorts of reasons why the code you've published will fail.
C.
用这个。
这里 ABSPATH = WordPress 物理根目录路径,末尾带有斜杠
use this.
here ABSPATH = WordPress physical root directory path with trailing slash