0

I have problems to define the right path to the root and bootstrap data in php.

My structure (Windows) looks like the following.

  • C:\xmapp\

    • htdocs\
      • testdrupal\
        • test.php
    • includes\
      • bootstrap.inc
    • sites\
      • default\
        • settings.php

The content of the test.php file is the following.

<?php

  chdir('C:\xampp\htdocs\testdrupal\');
  // Bootstrap Drupal up through the database phase.
  include_once('./includes/bootstrap.inc');
  drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
?> 

When I run it, I get the following error.

Parse error: syntax error, unexpected '/' in C:\xampp\htdocs\testdrupal\test.php

What am I doing wrong?

2
  • 1
    Why do you need a standalone script? If it's for something that's access through the browser, write a module. For a script that is executed on the console, I recommend looking into writing drush commands. – Berdir Feb 16 '12 at 10:14
  • 1
    possible duplicate of Properly bootstrap drupal for standalone PHP file? – Mołot Aug 14 '15 at 11:02
1

As reported in the PHP documentation, "[to] specify a literal single quote, escape it with a backslash (\)." In the string you wrote ('C:\xampp\htdocs\testdrupal\') the single quote character is escaped, which means you are saying to PHP "this is not the string delimiter; it is part of the string."

The string that PHP sees is the following one.

'C:\xampp\htdocs\testdrupal\');
// Bootstrap Drupal up through the database phase.
include_once('

When it parses the rest of the code, it finds the . which is the string concatenation operator, and a slash, which doesn't have any meaning for PHP at that point, as it is expecting a literal string, a constant name, a variable name, or a function/method call.

The second error you get about an undefined constant (DRUPAL_ROOT) is because Drupal 7 uses that constant, but the code you are using to bootstrap Drupal doesn't define it. See the code in the index.php file to understand where to define that constant and which value to assign it.

/**
 * Root directory of Drupal installation.
 */
define('DRUPAL_ROOT', getcwd());

require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
menu_execute_active_handler();

Drupal 7 doesn't rely on the current directory to find the directory where it has been installed, but it uses that constant every time it includes a PHP file. That is what module_include_file() does, for example.

function module_load_include($type, $module, $name = NULL) {
  if (!isset($name)) {
    $name = $module;
  }

  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;
    }
  }
  return FALSE;
}

Your code should be similar to the following one.

define('DRUPAL_ROOT', 'C:\xampp\htdocs\testdrupal');

// Bootstrap Drupal up through the database phase.
 require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
1

Check the first line (where you have ..testdrupal\');

The \ before the ' is escaping the ' so it's as if it's not there (sorry, it's tricky to explain without my morning coffee).

With the character escaped, the line is being read as follows:

chdir('C:\xampp\htdocs\testdrupal'./includes/bootstrap.inc');

Which is where your error is most likely coming from.

Try changing the first line to the following:

chdir('C:\xampp\htdocs\testdrupal\\');

The extra \ means that you are escaping the following character so the line will be read the way you want it to be.

There's a quick explanation of escape characters here.

1
  • Thanks for your answer...now it grives me more errors: Warning: chdir() [function.chdir]: No such file or directory (errno 2); Notice: Use of undefined constant DRUPAL_ROOT - assumed 'DRUPAL_ROOT' in C:\xampp\htdocs\testdrupal\includes\bootstrap.inc on line 2203 .... i guess its more the second part with the include once ?? – Jurudocs Feb 16 '12 at 9:06

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.