I have many PHP files in
/
/client/
/user/
/config/
etc...
I want all my files to include /user/codestart.php. (Lots of functions etc)
Therefore:
- All files in
/haveinclude("./user/codestart.php"); - All files in
/user/haveinclude("codestart.php"); - All files in
/client/haveinclude("../user/codestart.php");
The problem is that /user/codestart.php has include("../config/config.php"); (The MySQL ID and password)
When a file in / runs, such as /index.php, it includes ./user/codestart.php.
Then /user/codestart.php includes ../config/config.php, but it cannot see it, because it thinks it is calling it from /, instead of from /user/.
If I change
include("../config/config.php") to be
include("./config/config.php")
that fixes it for / files, but breaks it for/user/ and/client/ files.
Bottom line is that when one PHP file includes another file, PHP thinks it is operating from the location of the original file, not the calling file.
I need to use relative paths, not absolute paths. Absolute paths will not work in my situation.
Is there any way to solve this?
One way to deal with this is this:
Have a central configuration file (e.g.
/myproject/config/bootstrap.phpIn that configuration file, define a global root for your application. E.g.
Include that configuration file in every PHP file. E.g.
Whenever you reference some other file, use
Voilá – you have a fixed point in space (
APP_ROOT) and can reference everything relative to that, no matter which directory you are in.