Reputation: 386
When I include a file like this: include("/testsitedir/include/subdir/inc.php");
on a file named this.php
within the testsitedir
folder, I get the typical failed to open stream: No such file or directory in ...\testsitedir\this.php
error.
However, when I write a simple HTML anchor tag: <a href="/testsitedir/include/subdir/inc.php">Test Link</a>
(URL is identical to the include's) The browser automatically resolves the URL for me and knows that the /
indicates my site root.
How, then, can I make my includes resolve the URL in the same fashion that the HTML anchor tag does?
To get a better understanding, here is what I am trying to do. I have a header file located in a sub directory named content
which is within the site's root directory. I am trying to figure out how to include the header on every file using the same URL. My approach is to just use an absolute URL to the header file. The reason this has become an issue is because I want to be able to readily change the site's root using a single CONSTANT
. Also, the site's root directory will not always be the server's root directory. So when I want to put the site on a server in a sub directory www.example.com/view/site/siteroot/
I would define a constant SITE_ROOT
to be /view/site/siteroot
and include any files using include(SITE_ROOT."/includes/file.php);
But the include doesn't recognize the /
forwardslash as a site root delimiter. I was going to use the actual site name but I don't want to enable url_include
for security reasons.
Upvotes: 0
Views: 362
Reputation: 15616
Use the DOCUMENT_ROOT
variable defined in PHP:
include($_SERVER['DOCUMENT_ROOT'] . "/testsitedir/include/subdir/inc.php");
From the docs the variable contains:
The document root directory under which the current script is executing, as defined in the server's configuration file.
Upvotes: 1