I thought this would be easy, but can't figure it out. In a module how can I find the absolute path to the drupal directory? I can find the path to the module (both relative to drupal basepath and absolute), but not to drupal itself.

Comments

markj’s picture

at http://drupal.org/node/63579 but so far 0 comments have appeared.

You say that you can determine the path to the module... assuming it's in the standard module location, wouldn't the path to the drupal directory be the module path's parent? Mind sharing the progress you've made so far?

kuahyeow’s picture

kiz_0987’s picture

To get the full path to the module you can use dirname(__FILE__); -- this would return /path/to/drupal/modules/mymodule.

To get the relative path to the module you can use drupal_get_path('module', 'mymodule');

But nothing I've seen gets /path/to/drupal from a module. Any attempt to try to combine both the dirname(__FILE__); and remove the drupal_get_path('module', 'mymodule') part is tricky. A simple string replacement will fail on windows servers due to the use of '\' as a directory separator.

drawk’s picture

This topic is a few months old, but just in case anyone is searching realpath(".") works

kiz_0987’s picture

Thanks -- I still had no solution.

FreeFox’s picture

Even after a few years your info could be helpful ;)

Thanks
Jan

jaypan’s picture

global $base_url;

$base_url can now be used as the path to the Drupal directory.

Contact me to contract me for D7 -> D10/11 migrations.

alex.pilon’s picture

That is the base url. This topic is looking for a base file system path..

mossy2100’s picture

After looking everywhere, here's the function I wrote. You can chuck it anywhere in your site and it will work.
In the site I'm working on, the method of using DOCUMENT_ROOT and base_path() doesn't work for our XML feed, which does not run from index.php, but a script in a subfolder. Hence the need for this solution.

/**
* Get the true path to the root of the Drupal site.
* Better than using DOCUMENT_ROOT and base_path().
*/
function absolute_path_to_drupal() {
static $absolute_path_to_drupal = NULL;

if ($absolute_path_to_drupal === NULL) {
// Get the absolute path to this file:
$dir = rtrim(str_replace('\\', '/', dirname(__FILE__)), '/');
$parts = explode('/', $dir);

// Iterate up the directory hierarchy until we find the website root:
$done = FALSE;
do {
// Check a couple of obvious things:
$done = is_dir("$dir/sites") && is_dir("$dir/includes") && is_file("$dir/index.php");
if (!$done) {
// If there's no more path to examine, we didn't find the site root:
if (empty($parts)) {
$absolute_path_to_drupal = FALSE;
break;
}
// Go up one level and look again:
array_pop($parts);
$dir = implode('/', $parts);
}
} while (!$done);

$absolute_path_to_drupal = $dir;
}

return $absolute_path_to_drupal;
}

lars toomre’s picture

Thanks for sharing your solution to identifying the absolute path to the root of a Drupal installation. I have shared your solution in the issue http://drupal.org/node/752366#comment-3994742 where we are trying to solve a D6 issue where the D7 constant DRUPAL_ROOT is not defined.

Lars Toomre
Managing Director
Toomre Capital Markets LLC

http://lars.toomre.com/

sinasalek’s picture

A quick one line solution

$root_path=realpath(drupal_get_path('module', 'node').'/../../');

sina.salek.ws, Software Manager & Lead developer
Feel freedom with open source softwares

chrisblueearth’s picture

function dirroot() {
    if ( strstr( __FILE__, $_SERVER['DOCUMENT_ROOT']) ) {
        $dir = $_SERVER['DOCUMENT_ROOT'];
    } else {
        $dir = dirname(__FILE__);
        while ( ! file_exists($dir . '/install.php') ) {
            $dir = dirname($dir);
        }
    }
    return $dir;
}
jibus’s picture

Why not use DRUPAL_ROOT constant ?

robertdbailey’s picture

Jibus++

Rob Bailey

scottatdrake’s picture

DRUPAL_ROOT is not available for Drupal 6

trigve hagen’s picture

define('DRUPAL_ROOT', getcwd()); If you look at DRUPAL_ROOT it is defined by the function getcwd().