Wednesday, June 28, 2017 05:03 pm EDT

Drupal Root Path To the File System

Bri's picture

If you are ever in need of the root path of your drupal installation as a file system path, you can use the constant DRUPAL_ROOT in Drupal 7.

This might come in handy for requiring files or includes that have different paths for different environments, such as local, staging, or production sites.

For example, when working locally on a mac, the contents of DRUPAL_ROOT might contain something like:

/Users/bri/Sites/MAMP/mysite

But when I want to put that code in production on a linux box, then the content of the DRUPAL_ROOT might be:

/var/www/mysite/html

Given that, I could create a traditional include or require statement such as:

require_once(DRUPAL_ROOT . '/sites/all/modules/mymodule/includes/myinclude.php');

Granted this particular example could be done using module_load_include() as well, knowing this constant could come in handy at times.