Tyler Hughes
Tyler Hughes

Reputation: 612

PHP: Include inside include with different root

So I have:

Page A - /WWW/index.php

Page B - /WWW/folder/index.php

Page C - /WWW/assets/functions.php

I want to be able to use the same line of code to include Page C on Page A and Page B.

Right now for Page A: include "/assets/functions.php";

Page B: include "../assets/functions.php";

The "../" is what bugs me. Depending on how many times the file is nested in folder I have to add a "../"

I tried: include dirname(__FILE__) . "/assets/functions.php";

But it works on Page A and not Page B.

Also in the function.php I want to be able to include other PHP files.

Upvotes: 2

Views: 131

Answers (2)

R00We
R00We

Reputation: 1981

try it

require rtrim(dirname(__FILE__), '/\\') . DIRECTORY_SEPARATOR . 'my_file.php';

Upvotes: -1

Aelios
Aelios

Reputation: 12157

require_once($_SERVER["DOCUMENT_ROOT"]. "/assets/functions.php");

require_once is used to include your file once.

$_SERVER["DOCUMENT_ROOT"] is used to get your root directory.

Upvotes: 5

Related Questions