7 common.inc base_path()
4.7 common.inc base_path()
5 common.inc base_path()
6 common.inc base_path()
8 common.inc base_path()

Returns the base URL path (i.e., directory) of the Drupal installation.

base_path() adds a "/" to the beginning and end of the returned path if the path is not empty. At the very least, this will return "/".

Examples:

24 calls to base_path()
AJAXFrameworkTestCase::testAJAXRender in modules/simpletest/tests/ajax.test
Test that ajax_render() returns JavaScript settings generated during the page request.
color_scheme_form_submit in modules/color/color.module
Form submission handler for color_scheme_form().
CommonURLUnitTest::testUrl in modules/simpletest/tests/common.test
Test url() with/without query, with/without fragment, absolute on/off and assert all that works when clean URLs are on and off.
drupal_add_js in includes/common.inc
Adds a JavaScript file, setting, or inline code to the page.
FileURLRewritingTest::testPublicCreatedFileURL in modules/simpletest/tests/file.test
Test the generating of rewritten public created file URLs.

... See full list

2 string references to 'base_path'
template_preprocess_maintenance_page in includes/theme.inc
Process variables for maintenance-page.tpl.php.
template_preprocess_page in includes/theme.inc
Preprocess variables for page.tpl.php

File

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

Code

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

Comments

This function is for URLs. Are you looking for the path to the Drupal installation on the server's filesystem? Try the DRUPAL_ROOT constant. (Note that, unlike what's returned by this function, there won't be a / at the end of that path.)

There is catch here, for example if you want to use get url of http://example.com/files/logo.png, than use

$url = base_path() .'files/logo.png';

If you write this

$url = base_path() .'/files/logo.png';

It will return - http://files/log.png. which is incorrect.

Technically, it will return //files/log.png, which browsers will interpret as such: //files/log.png

$GLOBALS['base_url'] => http://example.com/drupal
base_path() => /drupal/
request_uri() => /drupal/documentation?page=1
request_path() => documentation
current_path() => node/26419

Thank you chx! this is really useful!!

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

<?php
 
/**
   * 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 (which 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'];
  }
?>

I notice that "base_path() . drupal_get_path('theme', 'themename')" and $parent_root both can get the current theme's directory, is there any difference? Which one methord is the best practise, please?

Sorry, just notice $parent_root is custom varialbe, which is same as base_path() . drupal_get_path('theme', 'themename');

I wonder if this method is the best practice to import css & js files from template, please?

when you want to set an image in background of your page and this image is being uploaded by editing the page then you may face some issues. All the pages of my website has diff-2 backgrounds and my client can change it any time just by editing the page. i added a custom img field and created node.tpl.php files to retrieve the image path and then i used that variable in my .tpl.php files.

I noticed that images were not appearing when two issues are there, 1. if image name contains any white space. 2. when url is not very simple e.g. if url is suppose 'your_domain/courses' then img was showing and if url is 'ur_domain/web/courses' then not.

I found this below given solution, plz see it.

Plz let me know if you have any doubts.