How to get the absolute path to the sites/all folder in drupal?

15,684

Solution 1

You can use the drupal global $base_url to get the absolute path of the installation and append the absolute path of your image to it.

See the Drupal Documentation to this topic.

You can also use base_path() to get the path.

Solution 2

It's important to note that:

  • base_path() returns the relative path to the folder where your drupal website is installed.
  • $base_url returns your domain path without a trailing slash.

So to be certain to have the right absolute path, you have to use both of them:

//example to get an image absolute path
global $base_url;

$image_path = $base_url . base_path() . path_to_theme() . '/images/my-image.png';
//$image_path will output something like: "http://www.my-domain.com/sites/all/themes/your-theme-name/images/my-image.png"

NB: path_to_theme is like $base_url, no trailing slash.

Share:
15,684
perpetual_dream
Author by

perpetual_dream

I am the co-founder and the web development specialist at Beyond Designs, a web development shop located in Jerusalem. In addition, I constantly blog about web hosting, domain names and CMS on Linux Hosting.

Updated on June 04, 2022

Comments

  • perpetual_dream
    perpetual_dream almost 2 years

    I want to display two different logos on my drupal site: one for the front page and one for the inner pages.

    if (drupal_is_front_page()) { 
        $variables['logo']=drupal_get_path('theme', 'hanna') . '/images/logo.png';
      } 
       else {
        $variables['logo']=drupal_get_path('theme', 'hanna') . '/images/logo-inner.png';    
      }
    

    This works just fine for the front page and any page which has websiteurl/page as URL; however, it doesn't work for the pages where the URL is similar to websiteurl/custompath/page.

    I think the problem lies in the relativeness of the pages which have a custom URL path.

    Do you have any idea?

  • perpetual_dream
    perpetual_dream over 12 years
    yea, I have used base_path().drupal_get_path('theme', 'hanna') . '/images/logo.png';
  • anou
    anou about 6 years
    NOTA BENE: I found today that path_to_theme will return the path of where the template is defined. For exeample if the theme function is from a module, then it will return the module path and not the theme path...