Software engineer, data guy, Open Source enthusiast, New Hampshire resident, husband, father. Fan of guitars, hiking, photography, homebrewing, sarcasm.
Drupal 6: Creating a Batch API operation to parse a large CSV file
In this code snippet I’ll show how you can parse a (large) CSV file using Drupal’s Batch API. The purpose of batching an operation is to avoid PHP memory limits and time outs. Before you begin, I recommend reviewing the following two articles. Be sure to review the additional batch parameters outlined in the documentation, you might need to use them.
We can start by defining the arguments that will be passed into the batch_set() function. For this example, I added this code in an arbitrary page callback function.
Next we’ll define the batch callback function. This function will be called repeatedly until the $context[‘finished’] variable is set to “1”.
The batch operation will be called until the end of the CSV file is reached. The $context variable is passed by reference into the batch callback so you can maintain data through each iteration; in this case, the position of the file pointer. When the batch operation is complete, the user will be redirected to the batch_process() path argument.
It’s important to read the full Batch API documentation so you can take advantage of its additional features: finished callback, init_message, progress_message, error_message, etc.