function parse_invoices($url, $xml_file, $username, $pw, $inventories)
{
    if ($xml_file != NULL) {
        $results = array();
        //Checks if each of the products is available online
        foreach ($xml_file->order as $order) {
            //Bigcommerce's status for completed order is '10'
            if ($order->status_id == 10) {
                $xml_part1 = '<?xml version="1.0" encoding="UTF-8"?>
                      <invoice>
                        <invoice_date>' . $order->date_shipped . '</invoice_date>
                        <reference>' . $order->id . '</reference>
                        <invoice_lines type="array">';
                //Gets products included on invoice
                //Uses product list link
                $xml_part2 = get_order_products($order->products->link, $inventories);
                $xml_part3 = '</invoice_lines>
                        <payments type="array">
                        <payment>
                          <amount>' . $order->total_inc_tax . '</amount>
                        </payment>
                        </payments>
                      </invoice>';
                //Complete xml_file to be posted
                $xml = $xml_part1 . $xml_part2 . $xml_part3;
                //Checks if product is available in Imonggo
                if ($xml_part2 !== 0) {
                    $result = post_file($url, $xml, $username, $pw);
                }
            }
        }
        //end of foreach
    }
}
<?php

require_once '../lib/lib.everything.php';
$context = default_context(True);
////
// Path
////
$mbtiles_filename = basename($_FILES['uploaded_mbtiles']['name']);
$target_mbtiles_folder = "mbtiles/";
$target_mbtiles_path = $target_mbtiles_folder . $mbtiles_filename;
////
// Content
////
$mbtiles_content_bytes = file_get_contents($_FILES['uploaded_mbtiles']['tmp_name']);
$mime_type = 'application/octet-stream';
// Post the file
post_file($target_mbtiles_path, $mbtiles_content_bytes, $mime_type);
$filename = explode('.', basename($_FILES['uploaded_mbtiles']['name']));
$slug = $filename[0];
$mbtiles_url = 'http://' . get_domain_name() . get_base_dir() . '/mbtiles.php/' . $slug . '/{Z}/{X}/{Y}.png';
$mbtiles = add_mbtiles($context->db, $context->user['id'], $mbtiles_url, $mbtiles_filename, 'files/' . $target_mbtiles_path);
$display_mbtiles_url = 'http://' . get_domain_name() . get_base_dir() . '/display_mbtiles.php?id=' . urlencode($mbtiles['id']) . '&filename=' . urlencode($slug);
header("Location: {$display_mbtiles_url}");
download_data($webDirectory, $locationsFile, $localDirectory);
download_data($webDirectory, $segmentsFile, $localDirectory);
//Add contents to feed.
$contents = addHeader($dateArray);
fwrite($xmlHandle, $contents);
$contents = addPrecincts(file_get_contents($localDirectory . $precinctsFile));
fwrite($xmlHandle, $contents);
$contents = addLocations(file_get_contents($localDirectory . $locationsFile));
fwrite($xmlHandle, $contents);
$contents = addSegments(file_get_contents($localDirectory . $segmentsFile), $xmlHandle);
fwrite($xmlHandle, $contents);
$contents = addFooter();
fwrite($xmlHandle, $contents);
zip_file($fname, $fnameZip);
if ($autoFTPupload) {
    post_file($ftp_directory . $fnameZipND, $fnameZip, $ftp_server, $ftp_user, $ftp_pass);
}
addLogMessage("DONE!");
//Close handles
fclose($logHandle);
fclose($xmlHandle);
/**
 * Download the data. Put in local directory
 * See decriptions of inputs above 
 */
function download_data($webDirectory, $dataFileName, $localDirectory)
{
    addLogMessage("Start download of " . $dataFileName);
    if (!@copy($webDirectory . $dataFileName, $localDirectory . $dataFileName)) {
        $errors = error_get_last();
        addLogMessage("COPY ERROR: " . $errors['type']);