8.5.x common.inc base_path()
8.0.x common.inc base_path()
8.1.x common.inc base_path()
8.2.x common.inc base_path()
8.3.x common.inc base_path()
8.4.x common.inc base_path()
8.6.x common.inc base_path()
4.7.x common.inc base_path()
5.x common.inc base_path()
6.x common.inc base_path()
7.x common.inc base_path()

Returns the base URL path of the Drupal installation. At the very least, this will always default to /.

20 calls to base_path()
color_scheme_form_submit in modules/color/color.module
Submit handler for color change form.
drupal_add_js in includes/common.inc
Add a JavaScript file, setting or inline code to the page.
drupal_build_css_cache in includes/common.inc
Aggregate and optimize CSS files, putting them in the files directory.
drupal_get_css in includes/common.inc
Returns a themed representation of all stylesheets that should be attached to the page.
drupal_get_js in includes/common.inc
Returns a themed presentation of all JavaScript code for the current page.

... See full list

File

includes/common.inc, line 1798
Common functions that many Drupal modules will need to reference.

Code

function base_path() {
  return $GLOBALS['base_path'];
}

Comments

rsevero’s picture

base_path returns the base URL path, not the local path.

Apparently $_SERVER['DOCUMENT_ROOT'] isn't safe as there seems to be situations where it is not set, see http://drupal.org/node/518460 and http://drupal.org/node/329710 for some discussions about it.

The proper way to set this would probably be something like

$GLOBALS['base_local_path'] = dirname(__FILE__);

in main index.php file.

As I won't touch it here is a fix that JUST WORKStm (code from http://drupal.org/node/518460#comment-1808184):

/**
 * Get the document root for the current Drupal installation.
 * $_SERVER['DOCUMENT_ROOT'] is not reliable across all
 * systems, so we need a way to get the correct value.
 *
 * @return (string)
 */
function YOURMODULE_document_root() {
  $absolute_dir = dirname(__FILE__);
  $relative_dir = drupal_get_path('module', 'YOURMODULE');
  return substr($absolute_dir, 0, -1 * (1 + strlen($relative_dir)));
}
bloke_zero’s picture

I was having difficulties grabbing the right location of the files directory on localhost (MAMP) and production - this solved it - thanks.

alpapan’s picture

A non-module specific (and simpler) way would be
return getcwd().'/';
(see http://drupal.org/node/363013)
or
return realpath('.'.base_path()).'/';
in case we have actually chdirred...

Indeed, many people ask for the root and many incorrectly answer that base_path() would be the solution. Perhaps we can have a Drupal function $base_root...?

peterx’s picture

base_path() and $base_path return / in my sites. No use when trying to read a file. realpath('./') worked. It picks up the path to index.php. I can then append the path provided by drupal_get_path().

Radon8472’s picture

I wrote a small function, what finds the path by walking step for step up, until its in the root directory and finds the relative path to bootstrap.inc.

  /**
   * Returns the base filesystem path of the Drupal installation.
   *
   * @author: Radon8472
   * @version: 1.0 (2013-10-19)
   *
   * @return string: absolute local filesystem path of the Drupal installation.
   */
  function filesystem_base_path()
  {
    if(!isset($GLOBALS['filesystem_base_path']))
    {
      $search = "includes".DIRECTORY_SEPARATOR."bootstrap.inc";

      // Walk directories up until we find the $search-path (wich is relative to root)
      for($path=dirname(__FILE__); !file_exists($path.DIRECTORY_SEPARATOR.$search); $path.= DIRECTORY_SEPARATOR."..")
      {
        // no need to do anything
      }
      // store the path if it one was found
      $GLOBALS['filesystem_base_path'] = realpath($path);
    }
    return $GLOBALS['filesystem_base_path'];
  }