In my project I want to provide path of some files from root directory. Path has repeated 4 to 5 time in one module so I am thinking that put one global variable and access that variable in same module?

I have try (dirname(__FILE__).'\sound\new'); realpath; drupal_realpath ; pathinfo; .

3 Answers 3


In Drupal 7 you can use variable_set and variable_get to make the variable global.

  • Set the variable
    • variable_set('MYMODULE_PATH', base_path() . drupal_get_path('module','YOUR_MODULE_NAME'));
  • Get the variable
    • variable_get('MYMODULE_PATH');

Also you can assign multiple values the variable, if you dont want to create multiple variables

      'm1' => base_path() . drupal_get_path('module','YOUR_MODULE_NAME'),
      'm2' => base_path() . drupal_get_path('module','YOUR_MODULE_NAME'),
  $values = variable_get('MYMODULE_PATHS');
  //Read as $values['m1'], $values['m2'] ..

You can get the path using drupal default function drupal_get_path();

It return path to any module or theme from root of installation.

If i want to access path of any module e.g. mymodule

Then i just need to use,

$path = drupal_get_path('module','mymodule');   //path to module directory

If i want to access path of my theme e.g. mytheme

$path =  drupal_get_path('theme','mytheme');   //path to theme directory

Now you can make the path variable global..

  • Thakns its working with this...
    – kkalaskar
    Sep 5, 2014 at 4:33

You can declare your global variables with multiple ways: for ex: in settings.php file - $conf['some_name'] = 'some_value', or with variable_set and variable_get (these functions store variable in db instead of defining them as global), or with builtin $GLOBALS array, etc..

But as Raj10 mentioned above, drupal_get_path($type, $name) is the best way to get the path to modules or themes..

  • Thaks for your time..!
    – kkalaskar
    Sep 5, 2014 at 4:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.