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

Justin picture Justin · Oct 18, 2010 · Viewed 7.4k times · Source

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)

Answer

Berdir picture Berdir · Jan 25, 2011

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