1

I'm still a bit new to PHP and stumbled across a weird error I can't fix. I have different util PHP files for different purposes, such as getting user data or basic util functions.

But I can't figure out why PHP does sometimes not find the path when using the require function.
In particular this example:

print '<a href="/api/connection.php">Huh</a>';

ob_start();
require "/api/connection.php";
ob_end_clean();

HTML shows a working link redirecting to the page, but PHP still can't find the connection file.

Here is my file tree:
Never gonna give you up

So why is that and how can I fix it? Does PHP have another way of linking files or is it something else?

Thanks in Advance

0

1 Answer 1

3

PHP paths are relative to the root of the filesystem, not the document root. Use $_SERVER['DOCUMENT_ROOT'] to get the latter.

require $_SERVER['DOCUMENT_ROOT'] . "/api/connection.php";
Sign up to request clarification or add additional context in comments.

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.