I am trying to move to Drupal from an old, custom CMS. I have a php script that connects to the mysql database and gets the titles and bodies for all of the pages. What I want to do is iterate through the results and add them as new nodes. Unfortunately I'm getting two different kinds of errors. If I do this:

        require_once './includes/bootstrap.inc';

I get this error message:

PHP Notice: Use of undefined constant DRUPAL_ROOT - assumed 'DRUPAL_ROOT' in ./includes/bootstrap.inc on line 2094
PHP Warning: require_once(DRUPAL_ROOT/includes/errors.inc): failed to open stream: No such file or directory in ./includes/bootstrap.inc on line 2094
PHP Fatal error: require_once(): Failed opening required 'DRUPAL_ROOT/includes/errors.inc' (include_path='.:/usr/share/pear:/usr/share/php') in ./includes/bootstrap.inc on line 2094

If I add in the specific path to the install like this:

        define("DRUPAL_ROOT", "/var/www/html/mydrupal/");
        require_once './includes/bootstrap.inc';

I get:

PHP Notice: Undefined index: REMOTE_ADDR in /var/www/html/mydrupal/includes/bootstrap.inc on line 2597
PHP Notice: Trying to get property of non-object in /var/www/html/mydrupal/modules/dblog/dblog.module on line 144
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'uid' cannot be null: INSERT INTO {watchdog} (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9); Array
[:db_insert_placeholder_0] =>
[:db_insert_placeholder_1] => php
[:db_insert_placeholder_2] => %type: !message in %function (line %line of %file).
[:db_insert_placeholder_3] => a:6:{s:5:"%type";s:6:"Notice";s:8:"!message";s:28:"Undefined index: REMOTE_ADDR";s:9:"%function";s:12:"ip_address()";s:5:"%file";s:40:"/var/www/html/mydrupal/includes/bootstrap.inc";s:5:"%line";i:2597;s:14:"severity_level";i:5;}
[:db_insert_placeholder_4] => 5
[:db_insert_placeholder_5] =>
[:db_insert_placeholder_6] => http:///importme.php?importme.php
[:db_insert_placeholder_7] =>
[:db_insert_placeholder_8] =>
[:db_insert_placeholder_9] => 1293647936
in dblog_watchdog() (line 155 of /var/www/html/mydrupal/modules/dblog/dblog.module).

My php script is in /var/www/html/mydrupal. I've tried looking through the api examples for 7, but they all assume you are writing a module to be used within Drupal, not a one time import script. Under 6, the require_once command should work, at least according to the examples.

Any ideas or a better way to do this?



vasi1186’s picture


can you try this: put your scrip in the root you your drupal installation, where also index.php is, and then:

define('DRUPAL_ROOT', getcwd());

require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
//Your code will go here.

This is also like drupal index.php works, so it should also work in your case.



rramsey’s picture

Nope, no go. I get the same long error message about REMOTE_ADDR, non-object on bootstrap.inc 2597, etc. Same as the long error in my first post. But here's the interesting thing. If I do:

php index.php

from the command line, I get the same error messages. But I can visit index.php in my browser and everything displays just fine. And if I put some print "hello world"; statements in the test code, when I visit the webpage, "hello world" prints out just fine

So it looks like there's something that's using the HTTP variables that are passed in from the webserver or webbrowser. I know I won't be able to run it down, but for a one time import I can get my code done and then just visit the php page in my browser.

vasi1186’s picture

So you get the error only when you run the script from command line? It yes, to avoid this you can just run the script from the web browser...


rramsey’s picture

Yes, the errors only happen from the commandline. I can probably work around that and just get the code and open the php file in the browser but there are problems with that:

1) You don't see the other php error messages that are important for debugging

2) Security. For this purpose it doesn't matter if someone loads the page since there's nothing they can hurt. But other people might find this a huge problem from a security standpoint.

vasi1186’s picture

For the second issue, with the security, you could just create a menu item and protect it with the drupal access system. And in the callback function, you can enable the error_reporting if you want...


jsims281’s picture

Fantastic! Works for me, the code I have is:

//set the working directory 
define('DRUPAL_ROOT', getcwd());

//Load Drupal
require_once './includes/bootstrap.inc';

//display a node
print '<pre>';
print '</pre>';
kotoponus’s picture

Hi, Vasi,

I am trying to "upgrade" from version 5 to 7. I have a few bootstrapped pages, and your comment was a strong pointer to remedy the issue for now. Thanks.

Original working version 5 was this (it was in one directory in the theme folder for good or bad):


And I just gave an definite orientation:


So that it defines DRUPAL_ROOT. Still it feels somehow wrong to define a constant in such a remote page, but it does the job.

newswatch’s picture

This fixed the problem.


Subir Ghosh

kevinsbath’s picture

Getting the same error on a 6.2 to 7.0 upgrade test.

It's a simple php script that accepts posted vars and creates a new node - using node_submit, so I'm loading the bootstrap.

egarias’s picture

here is my init_script()

function init_script() {
  global $adminuser;
  global $adminpass;
  ini_set('memory_limit', '512M');
if( ! function_exists("node_object_prepare")) {
      include_once(drupal_get_path('module', 'node') . '/node.pages.inc');
   } else {
  require_once 'modules/node/node.pages.inc';
  //Authenticate as user 1
  user_authenticate($adminuser, $adminpass);

After including:

define('DRUPAL_ROOT', getcwd());

I have

Fatal error: Cannot redeclare comment_page_title_alter() (previously declared in /xxx/public_html/sites/all/modules/page_title/modules/comment.page_title.inc:13) in /xxx/public_html/sites/all/modules/page_title/modules/comment.page_title.inc on line 27

Turning opportunities into business

kojow7’s picture

After playing around with some code, I finally came up with a solution for Drupal 7:

Below is a complete working example. Some things to note:

I copied this from Drupal 6 code found at http://workingdirectory.net/posts/2009/attach-file-to-node-drupal-6/ with some modifications as follows:

- I took out any code i didn't need (such as custom fields)
- For Drupal 7 I added: define('DRUPAL_ROOT', dirname(realpath(__FILE__)));
- For Drupal 7 I added: $_SERVER['REMOTE_ADDR'] = '';
- For Drupal 7 I modified the $node->body element, body is no longer a required field
- For Drupal 7 the built in content types are now "page" and "article", article has replaced "story"

     * This script is used to manually create nodes

    $uid = 1;

    // prevent this from running under apache:
    if (array_key_exists('REQUEST_METHOD', $_SERVER)) {
        echo 'nope.  not executing except from the command line.';

    // set HTTP_HOST or drupal will refuse to bootstrap
    $_SERVER['HTTP_HOST'] = 'example.org';
    $_SERVER['REMOTE_ADDR'] = '';

    define('DRUPAL_ROOT', dirname(realpath(__FILE__)));

    include_once 'includes/bootstrap.inc';
// create the node object

            $node = new stdClass();
            $node->type = 'page';
            $node->status = 1;
            $node->uid = $uid;
            $node->title = "I am the name";
            $node->body['und'][0]['format'] = 'full_html';
            $node->body['und'][0]['value'] = "I am the body";
            $node->created = time();
            $node->changed = $node->created;
            $node->promote = 1;
            $node->sticky = 0;
            $node->format = 1;
            $node->language = 'en';

            echo "Saved node: $node->nid\n";
akki123’s picture

One way is to check and set remote address to resolve this issue.

if ( !isset( $_SERVER['REMOTE_ADDR'] ) ) {


marcelomg’s picture

I'm running the above code from the command line and I get this error:

"You must use the -get flag to use UAParser.php from the command line"

Any ideas

kris-o3’s picture

this function was designed to, as the name suggests, override server environment variables prior to bootstrapping for command-line use.