PHP:包括内部包含不同的根

Tyl*_*hes 2 php directory include

所以我有:

第A页 - /WWW/index.php

第B页 - /WWW/folder/index.php

第C页 - /WWW/assets/functions.php

我希望能够使用相同的代码行在页面A和页面B上包含页面C.

现在对于页面A: include "/assets/functions.php";

第B页: include "../assets/functions.php";

"../"是我的错误.根据文件嵌套在文件夹中的次数,我必须添加"../"

我试过了: include dirname(__FILE__) . "/assets/functions.php";

但它适用于Page A而不是Page B.

同样在function.php中我希望能够包含其他PHP文件.

Ael*_*ios 5

require_once($_SERVER["DOCUMENT_ROOT"]. "/assets/functions.php");
Run Code Online (Sandbox Code Playgroud)

require_once 用于包含您的文件一次.

$_SERVER["DOCUMENT_ROOT"] 用于获取根目录.