Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to obtain the Drupal installed document root?

Inside of a Drupal module I need to obtain the base path where the Drupal site is installed.

For example, if the drupal site is installed at: www.example.com/mysite/ then I want to get '/var/www/myseite'

If it is installed in: www.example.com/ then I want '/var/www'

What is the proper Drupal way to get this? I want to avoid PHP's server variables, as I read they are unreliable.

(Drupal 6 and Drupal 7)

like image 277
Justin Avatar asked Oct 18 '10 03:10

Justin


2 Answers

In Drupal 7, there is a new constant called DRUPAL_ROOT which you can use for this.

like image 159
Berdir Avatar answered Oct 07 '22 20:10

Berdir


To get the complete document root of the file system I use:

$_SERVER['DOCUMENT_ROOT'] . base_path()

On a Linux server you will get something like:

/var/www/html/www.example.com/mysite/

If you only use "base_path()", you will get only:

/mysite/

Same on Drupal 6 and 7.

like image 41
FR6 Avatar answered Oct 07 '22 19:10

FR6