4

I'm coding a form in Drupal 7, and I want to 'include' a php file that adds my DB connection variables to my code.

I've tested my code in several ways, and I'm sure that the 'include' makes me get a WSOD when I run Cron in my site.

I've Googled about this and I tried:

include("/sites/all/libraries/php/connection.inc");

include("./sites/all/libraries/php/connection.inc");

include"./sites/all/libraries/php/connection.inc";

I also tried the above with '.php' extension.

And my last try:

define('__ROOT__', dirname(dirname(__FILE__)));
include(__ROOT__.'/sites/all/libraries/php/connection.inc');

Sometimes I get a WSOD when trying to run Cron, and sometimes my page styles get broken when I submit a form.

What is the correct way to include a PHP file manually in Drupal? Note that I don't want to use the 'Drupal way' to do this or to use the webform module. I want to code it manually with PHP.

moopet
  • 5,605
  • 1
  • 27
  • 35
donbuche
  • 89
  • 1
  • 3
  • 8

4 Answers4

12

The libraries directory should only be used to store files shipped as a library through the libraries module (see here https://drupal.org/project/libraries).

For both examples lets assume library.inc is our file and relative_path_to is the relative path based on the module directory to our library.inc file.

To just include a file you can use:

require(drupal_get_path('module', 'module_name') . '/relative_path_to/library.inc');

And to do it the Drupal way (https://api.drupal.org/api/drupal/includes!module.inc/function/module_load_include/7):

module_load_include('inc', 'module_name', '/relative_path_to/library');

Cheers, j

schlicki
  • 346
  • 4
  • 13
  • Actually, the Drupal way acts just as the first example you post out here. I would recommend the Drupal way definitely, since it uses DRUPAL_ROOT as root directory to include all files. And it seems a cleaner solution in a Drupal environment. Thanks for your answer! – Dazag Sep 24 '19 at 11:29
6

In my opinion these are correct Drupal (7) ways to include code:

  1. module_load_include(); function - Drupal API documentation - to include any PHP files from within your or other modules where needed,
  2. files[] = ... in your_module.info file to autoload include classes and interfaces within your own module - Writing .info file documentation.

Libraries module provides it's own way to include external PHP files from the libraries.

David Lukac
  • 622
  • 7
  • 16
5

Try this ways

1) require 'includes/iso.inc';
2) include_once  'includes/iso.inc';
3) include ('includes/iso.inc');

OR Drupal Ways

include_once DRUPAL_ROOT . '/includes/iso.inc';
Mehul Jethloja
  • 607
  • 4
  • 9
  • Hi there!, I used `include_once DRUPAL_ROOT . '/sites/all/libraries/php/connection.inc';` and it worked like a charm. Now, my form stores the form data in database and I can run Drupal Cron without any problems. Thank you very much guys. – donbuche Apr 15 '14 at 22:00
0

The second answer is the correct "Drupal way". The one that was accepted as the answer has many potential pitfalls if locations of things change. Using drupal_get_path is the best way. This is also especially true if you are trying to include a file that may not be fully bootstrapped during a module update hook.

  • 1
    This appears to be mostly a comment rather than an answer. References to "the second answer" are fragile as answer order may change. – moopet Jun 03 '16 at 13:26