-1

It has been covered many many times on several sites, explaining how to define DRUPAL_ROOT and bootstrap the page (whatever that actually means). However no one has ever said how to use it in a separate PHP file.

I am trying to use a php file as my data layer in a custom module. However when I use define('DRUPAL_ROOT', getcwd()) it comes back with the directory of the module (as expected). If I use $_SERVER['DOCUMENT_ROOT'] it comes back with the www HTML folder and not my drupal install folder.

If I add .base_path() it doesnt know what the hell that is! I can't just had code it because it's going to another IT team and I have no idea what their file structure is like.

Please, can anyone help! I am going insane!!

Its just a standard php file with some db_query's in. This really can't be as hard as I'm making it, can it?

2
  • Are you making a module or trying to have a completely separate php page that accesses Drupal in some way? – Darvanen Mar 16 '15 at 22:22
  • It's supposed to be a module that exports data from the database to Microsoft Word. However my manager insisted that I break all of the code out into separate php and inc files so there is nothing in the .module file. Would this make a difference as to why it doesn't understand DRUPAL_ROOT? – James Mar 17 '15 at 9:10
0

Why not use DRUPAL_ROOT instead of trying to define it in your php file?

DRUPAL_ROOT is already available at this point and it contains the path to your Drupal installation.

For example:

watchdog('testlog', DRUPAL_ROOT . 'test');

3
  • Thanks @sshrestha . However I tried using just DRUPAL_ROOT as: require_once DRUPAL_ROOT. '/includes/bootstrap.inc'; and it gave the error: Warning: require_once(DRUPAL_ROOT/includes/bootstrap.inc): failed to open stream: No such file or directory. Not sure if I am doing something wrong or if it makes a difference that it is not in the module file. – – James Mar 17 '15 at 9:44
  • @James I hate to be pedantic, but if what you typed in is verbatim command, then it is a syntax error. It should be require_once(ROOT.'/config.php'); Just want to make sure its not something that simple before thinking too far ahead. – sshrestha Mar 18 '15 at 13:02
  • Just tried require_once(ROOT.'/config.php') and that doesn't work either, as well as (DRUPAL_ROOT.'/config.php') – James Mar 18 '15 at 14:25
0

You need to define the DRUPAL_ROOT constant to point to the folder where your drupal installation is (in other words, on same folder where the index.php is located)

The bootstrap phase that you need load will depend on what do you need to do on your separated php script (db queries, session handling, multilanguage, ...).

On this answer you can see a small example on what I think you are looking to implement.

If you don't know where is going to be installed the Drupal site on other environments, you could use a relative path to define it, for example, if your script it's located in a module with this path:

sites/all/modules/custom/my_module

And your site has the same architecture on the other environments, you could set the DRUPAL_ROOT like this:

$drupaldir = realpath('../../../../../');
define('DRUPAL_ROOT', $drupaldir);

It's not elegant but it should work on any environment no matter on which folder is the drupal site.

3
  • Thanks @gerzenstl, however they have used "define('DRUPAL_ROOT', '/var/www/clients/client1/web15/web');" to define where the index.php is. I have done this and it works fine but as its going to another IT team that deals with many many drupal sites, I dont know what their file structure is like and therefore how to define it. I would ask them but they seem to get really stroppy if you ask them what seem to be really stupid questions. – James Mar 17 '15 at 9:17
  • It's not a stupid question to ask about where is located the index.php of a Drupal site/multisite installation. It's very hard to guess if all those sites are on a same location on the structure folder. – gerzenstl Mar 17 '15 at 13:57
  • 1
    If you have access to one of those sites as an admin user, you could go to admin/reports/status/php and check the DOCUMENT_ROOT value. – gerzenstl Mar 17 '15 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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