4

How can I include a PHP file located in my Drupal's home directory from a module?

3 Answers 3

8

Drupal 7 defines the constant DRUPAL_ROOT, which contains the name of the directory where Drupal is installed. It is used from Drupal functions, such as module_load_include() in a way similar to the following one:

if (function_exists('drupal_get_path')) {
  $file = DRUPAL_ROOT . '/' . drupal_get_path('module', $module) . "/$name.$type";
  if (is_file($file)) {
    require_once $file;
    return $file;
  }
}

In your case, you should use the following code:

require_once DRUPAL_ROOT . "/$filename";

The variable $filename contains the name of the file to load.

0
4

If I understand well, according my test is not needed indicate any path, just with the name of your file that is in your home directory. For example I have a file called testfile.php in my home directory, then from my module inside sites/all/modules I can use:

include ('testfile.php');

I hope information be useful.

0

I would use API module_load_include().

Edit : This API is meant for including PHP file located in module's directory.

1
  • You cannot use module_load_include() because the function looks for the file in a module's directory. The OP is asking about a file that is in the directory where Drupal is installed (what the OP calls "Drupal's home directory").
    – apaderno
    Oct 27, 2011 at 9:14

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.