How to write correctly path in include function?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SGUserFace
Forum Newbie
Posts: 2
Joined: Thu Jul 21, 2016 10:10 am

How to write correctly path in include function?

Post by SGUserFace »

How to write correctly path in include function?
User avatar
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?

Post by Christopher »

include() uses the filesystem path. You can echo $_SERVER["DOCUMENT_ROOT"]; to see the path to your HTML directory.
(#10850)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to write correctly path in include function?

Post 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";
SGUserFace
Forum Newbie
Posts: 2
Joined: Thu Jul 21, 2016 10:10 am

Re: How to write correctly path in include function?

Post 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)
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to write correctly path in include function?

Post by Celauran »

Have you tried using __DIR__ instead, then? You could use that to set a root path to which everything else is relative.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to write correctly path in include function?

Post 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.
User avatar
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?

Post 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.
(#10850)
Post Reply