Page 1 of 1

How to write correctly path in include function?

Posted: Thu Jul 21, 2016 10:37 am
by SGUserFace
How to write correctly path in include function?

Re: How to write correctly path in include function?

Posted: Thu Jul 21, 2016 4:45 pm
by Christopher
include() uses the filesystem path. You can echo $_SERVER["DOCUMENT_ROOT"]; to see the path to your HTML directory.

Re: How to write correctly path in include function?

Posted: Thu Jul 21, 2016 9:11 pm
by requinix
I almost always use the DOCUMENT_ROOT method.

Code: Select all

include $_SERVER["DOCUMENT_ROOT"] . "/path/according/to/web/root.php";
Sometimes I want to use a path relative to the current file. (Perhaps this file isn't executed from the web.) Always use __DIR__ to create an absolute path.

Code: Select all

include __DIR__ . "/../../relative/path/to/file.php";

Re: How to write correctly path in include function?

Posted: Sun Jul 24, 2016 7:11 am
by SGUserFace
Christopher wrote:include() uses the filesystem path. You can echo $_SERVER["DOCUMENT_ROOT"]; to see the path to your HTML directory.
requinix wrote:I almost always use the DOCUMENT_ROOT method.

Code: Select all

include $_SERVER["DOCUMENT_ROOT"] . "/path/according/to/web/root.php";
Sometimes I want to use a path relative to the current file. (Perhaps this file isn't executed from the web.) Always use __DIR__ to create an absolute path.

Code: Select all

include __DIR__ . "/../../relative/path/to/file.php";
$_SERVER["DOCUMENT_ROOT"] doesn't work on the server (only localhost)

Re: How to write correctly path in include function?

Posted: Sun Jul 24, 2016 8:01 am
by Celauran
Have you tried using __DIR__ instead, then? You could use that to set a root path to which everything else is relative.

Re: How to write correctly path in include function?

Posted: Sun Jul 24, 2016 9:16 am
by requinix
SGUserFace wrote:$_SERVER["DOCUMENT_ROOT"] doesn't work on the server (only localhost)
Then that's a server configuration issue that should be addressed.

Re: How to write correctly path in include function?

Posted: Sun Jul 24, 2016 5:27 pm
by Christopher
SGUserFace wrote:$_SERVER["DOCUMENT_ROOT"] doesn't work on the server (only localhost)
What does "doesn't work" mean? Not defined? That's not possible -- even on localhost. Otherwise the web server would not know where the files it is serving are located.