I am trying to get my include() functions in order but everytime I switch the syntax in one directory it messes up its subdirectory and vice versa.
I have a file called 'header.php' in my 'localhost/FTS/includes/header.php' folder. The 'FTS' folder has my index.php file so it is technically my root folder while I am testing.
In the file 'localhost/FTS/admin.php' I use the line include 'includes/header.php';
and it works fine but then when I go into the file 'localhost/FTS/admin/members.php' the include file is not found. Also inside of my 'header.php' file I include a couple more files from my root directory.
I just want all of my includes to work from each directory. Any ideas?
The include
statement includes files relative to the script executing include
, which is why you're seeing this issue. You have a couple of options available to you:
$_SERVER['DOCUMENT_ROOT']
, which if you're using Apache, will reference your document root directory. So something like include( $_SERVER['DOCUMENT_ROOT'] . "/includes/header.php" );
will appear to the script as an absolute path, so it will work if called from various places in your structure, but still be portable (note that it would require being called from a web server, this won't work if you're using your scripts through CLI).include( "includes/header.php" );
if the file you're including is in the includes
directory of the directory you're currently in, or include( "../includes/header.php" );
if the includes
directory is in the parent directory of the script being run.