Matt
Matt

Reputation:

Saving a Base64 string to disk as a binary using PHP

As a "look under the covers" tutorial for myself I am building a PHP script to gather emails from a POP3 mailbox. While attempting to make use of binary attachments I am stuck trying to figure out what to do with the attachment information.

Given a string that would be gathered from an email:

------=_Part_16735_17392833.1229653992102 Content-Type: image/jpeg; name=trans2.jpg Content-Transfer-Encoding: base64 X-Attachment-Id: f_fow87t5j0 Content-Disposition: attachment; filename=trans2.jpg

/9j/4AAQSkZJRgABAgEASABIAAD/4QxrRXhpZgAATU0AKgAAAAgABwESAAMAAAABAAEAAAEaAAUA AAABAAAAYgEbAAUAAAABAAAAagEoAAMAAAABAAIAAAExAAIAAAAUAAAAcgEyAAIAAAAUAAAAhodp

(...)

EAgEAgEAgEAgEAg8IBQRL/Lbe/tJrScHqZ2lkmE4XUP2XcSDZZ2VvZ28dtbsDIYmhkbRxAIJCAQC AQCAQf/ScyAQCAQCAQCAQCAQCAQCAQCAQCAQCAQf/9k= ------=_Part_16735_17392833.1229653992102--

Is there a way to save off the data to disk so that it would be in a usable format?

Upvotes: 14

Views: 47634

Answers (8)

esdebon
esdebon

Reputation: 2729

For big base64 strings from a DB you need use load()

$imgdata = $FromDB['BASE64']->load();
$imgdata =  base64_decode($imgdata);
file_put_contents($fileName, $imgdata);

Upvotes: -1

user2511210
user2511210

Reputation: 93

function base64_decode_file($data)
{
    if(preg_match('/^data\:([a-zA-Z]+\/[a-zA-Z]+);base64\,([a-zA-Z0-9\+\/]+\=*)$/', $data, $matches)) {
        return [
                'mime' => $matches[1],
                'data' => base64_decode($matches[2]),
        ];
    }
    return false;
}

Upvotes: 2

Mehdi Karamosly
Mehdi Karamosly

Reputation: 5438

I tried the below but did not work for me, was generating an empty image file.

file_put_contents('img.png', base64_decode($base64string));

this is how it worked for me:

$data = 'data:image/png;base64,AAAFBfj42Pj4';

list($type, $data) = explode(';', $data);
list(, $data)      = explode(',', $data);
$data = base64_decode($data);

file_put_contents('/tmp/image.png', $data);

I took the code from : How to save a PNG image server-side, from a base64 data string

Upvotes: 2

smartcoder
smartcoder

Reputation: 129

Chop off first and last line, base64decode and save under given filename.

done.

Upvotes: 0

Suman
Suman

Reputation: 9561

I had a similar situation, and this is what I did. As noted earlier, make sure to remove the extraneous lines before and after the base64 string.

<?php
$INPUT = "inputfile_base64_encoded.txt";
$OUTPUT = "output_decoded.zip";
$contents = file_get_contents($INPUT);
$bin = base64_decode($contents);
file_put_contents($OUTPUT, $bin);
?>

Upvotes: 0

Mike Boers
Mike Boers

Reputation: 6735

If you have a lot of data to write out that needs to be decoded before writing I would suggest using stream filters so decode the data as you write it...

$fh = fopen('where_you_are_writing', 'wb');
stream_filter_append($fh, 'convert.base64-decode');

// Do a lot of writing here. It will be automatically decoded from base64.

fclose($h);

Upvotes: 9

Milen A. Radev
Milen A. Radev

Reputation: 62573

While reinventing the wheel has some entertainment and educational value, I would try to resist the temptation: Mailparse, Mail_mimeDecode

Upvotes: 0

Paul Dixon
Paul Dixon

Reputation: 300835

Pass the data to base64_decode() to get the binary data, write it out to a file with file_put_contents()

Upvotes: 29

Related Questions