How to write correctly path in include function?
Moderator: General Moderators
-
SGUserFace
- Forum Newbie
- Posts: 2
- Joined: Thu Jul 21, 2016 10:10 am
How to write correctly path in include function?
How to write correctly path in include function?
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: How to write correctly path in include function?
include() uses the filesystem path. You can echo $_SERVER["DOCUMENT_ROOT"]; to see the path to your HTML directory.
(#10850)
Re: How to write correctly path in include function?
I almost always use the DOCUMENT_ROOT method.
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 $_SERVER["DOCUMENT_ROOT"] . "/path/according/to/web/root.php";Code: Select all
include __DIR__ . "/../../relative/path/to/file.php";-
SGUserFace
- Forum Newbie
- Posts: 2
- Joined: Thu Jul 21, 2016 10:10 am
Re: How to write correctly path in include function?
Christopher wrote:include() uses the filesystem path. You can echo $_SERVER["DOCUMENT_ROOT"]; to see the path to your HTML directory.
$_SERVER["DOCUMENT_ROOT"] doesn't work on the server (only localhost)requinix wrote:I almost always use the DOCUMENT_ROOT method.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 $_SERVER["DOCUMENT_ROOT"] . "/path/according/to/web/root.php";Code: Select all
include __DIR__ . "/../../relative/path/to/file.php";
Re: How to write correctly path in include function?
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?
Then that's a server configuration issue that should be addressed.SGUserFace wrote:$_SERVER["DOCUMENT_ROOT"] doesn't work on the server (only localhost)
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: How to write correctly path in include function?
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.SGUserFace wrote:$_SERVER["DOCUMENT_ROOT"] doesn't work on the server (only localhost)
(#10850)