I needed to generate Australia Post manifests for shipping e-commerce store orders. A PHP controller action written using the CodeIgniter framework creates required CSV files. Ideally this uses an algorithm to optimize a packing plan based on weight, cubic-size, and packing materials available. You know, if you’re feeling ambitious enough to solve the Knapsack Problem. In my case, users instead tick boxes to indicate parcel placement for each item. Not a terrible proposition with less than a dozen items.

With the packing plan decided, CSV files generate for the format accepted by the AusPost API. Private method _download_csv_zip() accepts two parameters:

  • $zip_filename_stub, a filename describing the shipped order/service. e.g., ‘order_39493’
  • $csv_files, associate array of files and their CSV lines. e.g., array('order_39493_auspost.csv' => '"first column","345","third column"')

Custom ZIP creation in PHP

(Error handling below is generalized to simplify this post – throwing an Exception in PHP isn’t so bad.)

Instead use the CodeIgniter library

Immediately after completing this quick implementation I thought, “what functionality does CodeIgniter have for ZIP archives?” Yup, ZIP library. Condense that block above with four lines below.