包括父目录或其他目录中的文件

php
2022-08-30 10:01:10

我需要将父目录中的文件和其他子目录包含在子目录中。我以前通过简单地使用include('/rootdirectory/file.php')来完成它;但现在它似乎不起作用。

只是想知道我怎么能做到这一点,谢谢。

这是我的确切台词:

include('/forums/groups.php');

它给我这个错误(页面仍在运行):

警告:include(/forums/groups.php) [function.include]: 无法打开流: C:\xampp\htdocs\forums\blog\posts 中没有这样的文件或目录.php在线

警告:include() [function.include]: 未能打开“/论坛/组.php”进行包含(include_path=“)。;C:\xampp\php\PEAR') 在 C:\xampp\htdocs\forums\blog\posts.php 第 3 行


答案 1

include()它的亲戚采用文件系统路径,而不是相对于文档根目录的Web路径。要获取父目录,请使用../

include('../somefilein_parent.php');
include('../../somefile_2levels_up.php');

如果以 开头,则将使用绝对系统文件路径:/

// Full absolute path...
include('/home/username/sites/project/include/config.php');

答案 2

如果您的服务器未使用

include '../somefilein_parent.php'

试试这个(使用相对于脚本的父目录):

include __DIR__ . "/../somefilein_parent.php";


推荐