Home > front end >  PHP can't find path while HTML can
PHP can't find path while HTML can

Time:08-17

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

CodePudding user response:

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";
  •  Tags:  
  • php
  • Related