By kiz_0987 on
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
I asked a similar question three weeks ago
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?
realpath?
http://nz.php.net/manual/en/function.realpath.php
--
Cheers,
Thong
Tip: http://drupal.org/forum-posting
Website: http://www.edoodle.co.nz
Drupal for artists - demo at Website for artists
To get the full path to the
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.
This topic is a few months
This topic is a few months old, but just in case anyone is searching realpath(".") works
Thanks -- I still had no
Thanks -- I still had no solution.
old?
Even after a few years your info could be helpful ;)
Thanks
Jan
<?php global
$base_url can now be used as the path to the Drupal directory.
Contact me to contract me for D7 -> D10/11 migrations.
That is the base url. This
That is the base url. This topic is looking for a base file system path..
Solution
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;
}
Shaun Moss
shaun@astromultimedia.com
https://github.com/mossy2100/
Thanks for sharing mossy2100!!
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/
A quick one line
A quick one line solution
sina.salek.ws, Software Manager & Lead developer
Feel freedom with open source softwares
More succinct
DRUPAL_ROOT
Why not use DRUPAL_ROOT constant ?
re: DRUPAL_ROOT
Jibus++
Rob Bailey
DRUPAL_ROOT is not available
DRUPAL_ROOT is not available for Drupal 6
Root