16

This is what I have right now:

<?php
    define('DRUPAL_ROOT', '/var/www/clients/client1/web15/web');
    include_once(DRUPAL_ROOT . '/includes/bootstrap.inc');
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
//  drupal_flush_all_caches();  

    $users = views_get_view_result('userlist', 'uid_list');
    $i = 0;
    foreach ($users as $user) {
        $filename = 'sites/default/files/generated_pdfs/individual_pdf_report_' . $user->uid . '.pdf';
        generate_employee_pdf($user->uid, $filename);
        error_log('Generated ' . $filepath);
        $i++;
    }
    // create object
    $zip = new ZipArchive();
    // open archive
    if ($zip->open("sites/default/files/generated_zips/individual-reports.zip", ZIPARCHIVE::CREATE) !== TRUE) {
        die ("Could not open archive");
    }
    // initialize an iterator
    // pass it the directory to be processed
    $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("sites/default/files/generated_pdfs/"));
    // iterate over the directory
    // add each file found to the archive
    foreach ($iterator as $key=>$value) {
        $zip->addFile(realpath($key), $value->getFilename()) or die ("ERROR: Could not add file: $key");
    }
    // close and save archive
    $zip->close();
?>

I'm getting a bunch of errors from the above when run standalone. Works fine in my module. Some of the errors are:

Warning: Invalid argument supplied for foreach() in drupal_depth_first_search() (line 56 of /var/www/clients/client1/web15/web/includes/graph.inc).
Warning: Invalid argument supplied for foreach() in _module_build_dependencies() (line 229 of /var/www/clients/client1/web15/web/includes/module.inc).
Warning: include_once(sites/all/modules/views_bulk_operations/actions/archive.action.inc): failed to open stream: No such file or directory in views_bulk_operations_load_action_includes() (line 56 of /var/www/clients/client1/web15/web/sites/all/modules/views_bulk_operations/views_bulk_operations.module).
4
  • 1
    What's the point of a standalone php file if you do a full bootstrap?
    – googletorp
    Commented Apr 17, 2012 at 7:02
  • 2
    @googletorp, I use these from time to time to schedule things via a crontab when I want Apache access control and want to stay out of the Drupal cron mechanism. I would add though, that using a proper hook_menu() entry or a drush command is usually a better idea in most circumstances.
    – mpdonadio
    Commented Apr 17, 2012 at 13:43
  • 1
    Working on a site right now with several outside PHP files needing to utilize this type of functionality. I found this recently, maybe it will help: interworks.com/blogs/jkhalaj/2012/05/01/how-bootstrap-drupal
    – fndtn357
    Commented Jan 23, 2013 at 4:07
  • You can bootstrap drupal with drush src command Here are some explanations: oliverdavies.co.uk/blog/2013/11/dont-bootstrap-drupal-use-drush
    – ya.teck
    Commented Jan 9, 2014 at 11:45

4 Answers 4

14

By far the easiest way to manage the bootstrap is to use drush php-script, alias scr.

file.php:

<?php
echo l('Link', 'path');

Cli:

drush @alias scr file.php

Output:

<a href="/path">Link</a>

This way you need to deal with nothing with regard to the bootstrap, drush will solve it entirely for you, and gives you the ability to use it easily across sites thanks to aliases. (Might even work on remote sites, though I haven't tried that)

4
  • If you want to do this on a remote site, the script has to be remote (see drupal.stackexchange.com/questions/98098). If I ever get free time (ha!) will try a patch for this.
    – mpdonadio
    Commented Jan 9, 2014 at 14:53
  • This really is the simplest way to do it and should get more upvotes.
    – Bala Clark
    Commented Apr 23, 2014 at 9:43
  • What exactly is @alias supposed to do? I only get a Could not find the alias @alias. Unfortunately the link you provided is broken. Could you please fix it and expand your explanation?
    – leymannx
    Commented Nov 17, 2015 at 10:21
  • @leymannx Hey, sorry I didn't notice your question before. I've updated the link to the documentation. I've not expanded the alias part however, as drush aliases are really out of scope for this particular question.
    – Letharion
    Commented Feb 27, 2016 at 19:41
11

your standalone Drupal 7 bootstrap is not quite right.

Try this:

/**
 * Do not let execute this file from http request
 */
if (isset($_SERVER['REMOTE_ADDR'])) {
  print "...";
  exit(1);
}

/**
 * Initialize Drupal
 */
define('DRUPAL_ROOT', '/var/www/clients/client1/web15/web');
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
require_once DRUPAL_ROOT . '...'; // include required module files to work with here

drupal_override_server_variables(array('url' => 'http://www.example.com/batch.php')); // trick drupal to do bootstrap
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);

set_time_limit(4600); // adjust for your needs
ini_set('memory_limit', '3524M'); // adjust for your needs

Hope this helps.

4
  • Hmm. It's failing silently at this line: $users = views_get_view_result('userlist', 'uid_list'); Am I correct that your drupal_override_server_variables call should point at the location of my php file?
    – Joren
    Commented Apr 17, 2012 at 18:59
  • Yes You right. This bootstrap loads only database layer, but not modules. You must invoke module init hook for views to use it's functionality or include module file responsible for data processing here: require_once DRUPAL_ROOT . '...'; // include required module files to work with here
    – Zyy
    Commented Apr 18, 2012 at 6:53
  • I've included common.inc, views.module and ctools.module as the errors made it obvious they were needed. After these 3 I stopped getting errors and it just died silently. Not sure where to go from here.
    – Joren
    Commented Apr 18, 2012 at 18:01
  • Do printouts after each inclusion and see where it stuck. Then you may know what's wrong. As an option, try to do full bootstrap and see what's happens.
    – Zyy
    Commented Apr 19, 2012 at 11:47
3

this is enough:

set_include_path(get_include_path() . PATH_SEPARATOR .'/path/to/drupal/root/');
define('DRUPAL_ROOT', '/path/to/drupal/root/');
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
0

The last error

Warning: include_once(sites/all/modules/views_bulk_operations/actions/archive.action.inc): failed to open stream: No such file or directory in views_bulk_operations_load_action_includes() (line 56 of /var/www/clients/client1/web15/web/sites/all/modules/views_bulk_operations/views_bulk_operations.module).

is a bug in VBO. views_bulk_operations_load_action_includes() does not use DRUPAL_ROOT when using include_once. I would file a bug and fix it in VBO.

The other error (the 2 are related to each other) might be related to _system_rebuild_module_data() not using DRUPAL_ROOT when accessing modules .info file. I would put break points in system_rebuild_module_data() for the $modules variable and see if you can pin point it. Once again file a bug with drupal core as I don't see this issue in the issue queue.

Good luck!

0

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