How to write correctly path in include function?
Posted: Thu Jul 21, 2016 10:37 am
How to write correctly path in include function?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
include $_SERVER["DOCUMENT_ROOT"] . "/path/according/to/web/root.php";Code: Select all
include __DIR__ . "/../../relative/path/to/file.php";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";
Then that's a server configuration issue that should be addressed.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.SGUserFace wrote:$_SERVER["DOCUMENT_ROOT"] doesn't work on the server (only localhost)