Defining a Root Path

2019-07-01 18:39发布

问题:

I'm looking for a way to define a config variable as being the root path of my website. What's the nicest way to define this. I am using Codeigniter.

$config['root_path'] = ?

This is for my site_config file in my config folder.

/
/dev
/dev/application
/dev/application
/dev/application/config
/dev/application/config/site_config.php
/dev/system/
/dev/assets/
/public_html

回答1:

I think

$config['root_path'] = $_SERVER['DOCUMENT_ROOT'];

will give you what you're looking for, regardless of which script calls it from where.

On the other hand, I have approximately zero experience with codeigniter, which could negatively impact the efficacy of my suggestion.



回答2:

I usually use __DIR__. For example, if your configuration file is in the root directory you can use:

$config['root_path']=__DIR__;

Or if it's in a subdirectory of the project (for example /anyframework/config/config.php):

$config['root_path']=dirname(dirname(__DIR__));