Here is the path to the file with the include statement:
http://localhost/plugins/index.php
And here's the path to the file I'm including:
http://localhost/header.php
So when I call include('/header.php')
I get the failed to open stream warning message. But when I use include('../header.php')
everything works fine. However, I have files contained deep with directories, and I don't want to keep specifying relative paths one directory up at a time. I'm sure there must be a way to shorten the include argument other than using '/'
; If there is, what could that be?
include('/header.php')
looks in your root folder (literally /
on Unix, usually c:\
on Windows; this is an absolute include, not a relative one.
Your best bet for handling the nesting is to use absolute includes using the folder name of your web root (I don't know your platform or setup). If you don't know you might execute echo dirname(__FILE__)
from header.php to figure it out; usually /var/www/
on Linux or on Windows perhaps something crazy like C:/Program Files/Apache Software Foundation/Apache2.4/htdocs
.
Once you know that, absolute includes like include('/var/www/header.php')
would work regardless of how your files are nested.
Hope that helps.
$_SERVER['DOCUMENT_ROOT'] can be used more ideally like so:
include $_SERVER['DOCUMENT_ROOT'] . '/header.php';
This is more ideal than using other absolute urls like include '/usr/local/apache2/htdocs...
which tie your site’s code to your web server configuration.
If you use include()
on the script the file will be included.The pathname on unix starts with /
and in windows it is a colon.
Also see the function realpath() which returns an absolute pathname.
Or you can just use like
include $_SERVER['DOCUMENT_ROOT'] . "/includefolder/thefile.php";