I'm writing a simple module that will load images from a directory and display one at random. How can I get the path to the base directory on the server that houses my drupal install? This is different from $base_url, which would return a URL.

Thanks,
Kevin

Comments

nofue’s picture

Has this ever been resolved? I'm wondering myself how to access the base directory without getting into nasty open_base_dir warnings or file_exists == false conditions writing PHP… Any hints?

Norbert

-- form follows function

Aurian Noreinor’s picture

nofue’s picture

Thanks, but $base_path doesn't anything. The problem is the basepoint of referral: from "outside" the installation has a root of f.e. http://www.myserve.tld and $base_path refers to that 'root'. But PHP uses file read/write paths from the absolute server path, which maybe something like /var/www/domain17/htdocs/

I "solved" the problem with a bad hack in the short term, until I come up with something more universal…

function _menu_to_picture($directory) {
    $thePath = file_directory_path() . $directory;
    $filename = $thePath . menu_get_active_title();
    
    $pixfile = $filename . '.png';
    if (file_exists($pixfile))  return $pixfile;
    
    $pixfile = $filename . '.gif';
    if (file_exists($pixfile))  return $pixfile;
    
    $pixfile = $filename . '.jpg';
    if (file_exists($pixfile))  return $pixfile;
    
    return $thePath . 'default.png';
}

This code is called from within the theme page.tpl.php

  $pixfile = 'http://www.domain.tld/' . _menu_to_picture('/graph/'); 

  print '<img class="titelbild" src="' . $pixfile . '" width=864 height=367 alt="" />'

Using the URI hardcoded in the theme isn't exactly what I like, but so far it works. If anybody has an idea how to find the current URI of the site (i.e. the full http://www.domain.tld/ thing), let me know :)

Norbert

-- form follows function

x3cion’s picture

realpath(".");

should work.

Try it.

operations’s picture

@x3cion that works fine for me ! thanks

ddd2500’s picture

This question puzz me 1hour, and it's work now. thx.

jaypan’s picture

realpath(__FILE__);

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

Speicus’s picture

Use DRUPAL_ROOT constant for that.