This seems like a stupid question to me, but I have looked around quite a bit and can't find it.. How do I get the path to the drupal root directory? (there must be a function or predefined variable to do this, right??)

thanks.

Comments

zaboomafoozarg’s picture

I think base_path() should do the trick.

tstclair’s picture

Yeah, I thought that it would too, but for me it only returns '/', which is not the location of my drupal root directory. Is there some sort of file or environment variable I have to set?

zaboomafoozarg’s picture

well there is always $_SERVER['DOCUMENT_ROOT'] from the PHP, could use that.

dannygoh’s picture

Hi tstclair,

This is how i use $base_url for similar purposes:

 
  /* first bring it to scope */
    global $base_url;

    $html_string = '<a href=' . $base_url . '/node/42042></a>';

And if your website is 'http://drupal.org' (for example) then you will get http://drupal.org/node/42042

Hope this will help.

samjosein’s picture

I found this one very useful.

I got it printed on my login page.

<form action="<?php global $base_url;print $base_url; ?>/node?destination=node" method="post" id="user-login-form" accept-charset="UTF-8">

dkinzer’s picture

getcwd()

Gets the current working directory.

dahousecat’s picture

Use DRUPAL_ROOT.

It is defined in index.php as:

define('DRUPAL_ROOT', getcwd());

This is in fact the first line that runs on every single page request.

siliconmeadow’s picture

DRUPAL_ROOT was introduced in Drupal 7, well after this question was asked.

ravi_admec’s picture

hello
I used DRUPAL_ROOT and it gave me /home/sairam1818/public_html/www.devotionalindia.com/newdevotionalindia

You can see that '/home/sairam1818/public_html/' needs to be replaced by 'http://'

so is there any function to get http://www.devotionindia.com/newdevotionalindia or i need to use str_replace function to deal with it.

one more suggestion i need:
what method should i use to get the above url if i am wrking with block.

jaypan’s picture

is there any function to get http://www.devotionindia.com/newdevotionalindia

  global $base_url;

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

AmolB’s picture

This worked for me.

ravi_admec’s picture

I noticed that images were not appearing when following issue is there,
1. if image name contains any white space.
2. DRUPAL_ROOT gives '/home/sairam1818/public_html' instead 'http:/'

I found this below given solution, plz see it.

echo 'background:url('.str_replace('/home/sairam1818/public_html','http:/',DRUPAL_ROOT).'/sites/default/files/'.str_replace(' ', '%20', $getImg).')

It manages whitespace in the image path and replaces the undesired value with http:/
Plz let me know if you have any doubts.

ravibarnwal’s picture

You should read these documention Global variable drupal 7

srikanth.g’s picture

global $base_url;print $base_url;
Website logo always pointing to home page dynamically,this code works fine with drupal7.

mattrock1’s picture

This works perfectly for me. Thanks srikanth.g!

graham leach’s picture

Interesting! Thanks!