Search code examples
phpups

Force a Generated UPS Label to Download


I'm using a third party php class to talk to UPS and generate a label. The generated label data comes in the Base64 format and I can display it like this.

echo '<img src="data:image/gif;base64,'.$label_data.'" />';

I'm just looking for a way to force this image to be downloaded (as a GIF) instead of just displaying it.

Any idea how this is possible?


Solution

  • Since you already have the data, just output the headers and then the data!

    <?php
    header('Pragma: public');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Cache-Control: private',false);
    header("Content-type: application/force-download"); // or application/octet-stream
    header('Content-Disposition: attachment; filename="ups.gif"');
    header('Content-Transfer-Encoding: binary');
    header('Connection: close');
    echo $label_data;
    exit;
    ?>