12

我的目录结构如下所示:

blog -> admin -> index.php
blog.php
db.php
functions.php

我一直在尝试includerequire真的)blog.phpadmin/index.php但面临很多错误。我正在学习 PHP 课程,并且讲师成功地做了同样的事情。

管理员/index.php:

require "../blog.php";

反过来,它的目录中还需要两个文件。

require "db.php";
require "functions.php";
4

4 回答 4

40

If you find that relative include paths aren't working as expected, a quick fix is to prepend __DIR__ to the front of the path you're trying to include.

require __DIR__ . "/../blog.php";

It's reasonably clean, and you don't need to modify the include path or working directory.

于 2012-12-16T05:40:52.700 回答
3

您需要include_path在 php.ini 中设置。

如果要在运行时设置它,请使用set_include_path().

于 2012-12-16T05:34:57.280 回答
2

如果要包含此文件db.php and functions.phpindex.php则必须编写此代码

require "../db.php";
require "../functions.php";

或者,如果您包含此文件,blog.php则编写此代码

require "db.php";
require "functions.php";
于 2012-12-16T05:31:52.870 回答
-3

我喜欢以chdir($_SERVER['DOCUMENT_ROOT']). 这使我能够为我的所有包含获得一个很好且合乎逻辑的基本路径。

于 2012-12-16T05:30:03.347 回答