I am developing a web application. contents are:
- root dir (/var/www/)
config.php
index.php
details.php
- admin dir (/var/www/admin)
admin.php
I have included config.php file into index.php, details.php in root directory using require_once(‘config.php’) as this file contains database passwords, styles, images directory paths..
how can i include that config files in my admin/admin.php file so that one config file can be used in anywhere(even in subdirectories) of my web application. Will it make any difference for the value of define('APP_BASE_PATH', dirname(__FILE__)); when same config file is used by all files in the web application.
if i am wrong somewhere then please get me right.
Update after clarification in comments: You are looking for a way to include a central configuration file from anywhere in your project’s folder structure.
@Col. Shrapnel shows one way,
DOCUMENT_ROOT. It’s the only way to use an “absolute” path from a nested folder structure. It has the limitation I describe above, but it’s fine otherwise.If you want maximum portability (i.e. the possibility to run the app with e.g.
www.example.com/myapp/version_1as its root directory), you would have to use relative references from within your folder structure to “climb down” to the config file, e.g.../../config.phpthat will work reliably too, although be a bit cumbersome e.g. if you move a script to a different folder and you have to update the relative path.