I have a file navbar.php which is in folder views/general. It includes a few relative path files ../controllers/file1.php etc..
I can only include the navbar.php file in other files in the same views/general folder. If I try to include it in a file outside that, like views/signup,
the include paths contained in the navbar.php (../controllers/file1.php etc), won't be relevant anymore.
How can i solve that, so navbar.php can be used from anywhere ?
You can also generalise everything from the root directory of your website hosting by using the
$_SERVER['DOCUMENT_ROOT']
variable which will be the directory where yourindex.php
should be located.Use as follows:
If you're using PHP 5.3+:
__DIR__ is a magic constant holding the absolute path of the current file.
If you're using earlier versions of PHP:
I had a similar challenge and created a single file that defines constants for all the relevant paths that I want to be able to call as-needed. I include this file in all my pages (I define the $urlRoot so that this will work in all environments and is moveable do different domains, etc):
File: pathData.php (added MENUDIR for your example):
Then in each file, I include that file with an include that includes the relative directory path. For example:
So in your case you could (depending on where your pathData file is):