2

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.

2 Answers 2

5
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.

Sign up to request clarification or add additional context in comments.

2 Comments

Errors are not displayed by default. I'm checking by have a variable on function.php and on both index.php and /folder/index.php I'm echoing it and neither is echoing.
Sorry, I should have had error reporting on in the first place. I would have figured it out without needing to ask this. I'm building a new site on my site under /new/ and I forgot to add that part before assets. I assumed it already new that as the root too.
-1

try it

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.