A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearSaving a Base64 string to disk as a binary using PHP - page 1
User InfoPosts
Saving a Base64 string to disk as a binary using PHP#1
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?

posted date: 2008-12-19 05:10:00


Re: Saving a Base64 string to disk as a binary using PHP#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-19 05:10:01


Re: Saving a Base64 string to disk as a binary using PHP#3
Chop off first and last line, base64decode and save under given filename.done.

posted date: 2008-12-19 05:20:00


Re: Saving a Base64 string to disk as a binary using PHP#4
Pass the data to base64_decode() to get the binary data, write it out to a file with file_put_contents()

posted date: 2008-12-19 05:28:00


Re: Saving a Base64 string to disk as a binary using PHP#5
While reinventing the wheel has some entertainment and educational value, I would try to resist the temptation: Mailparse, Mail_mimeDecode

posted date: 2008-12-19 11:11:00


Re: Saving a Base64 string to disk as a binary using PHP#6
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);

posted date: 2009-02-19 19:51:00


Re: Saving a Base64 string to disk as a binary using PHP#7
saved my day, thanks

posted date: 2012-02-14 06:05:00


Re: Saving a Base64 string to disk as a binary using PHP#8
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);?>

posted date: 2012-08-21 13:20:00


Re: Saving a Base64 string to disk as a binary using PHP#9
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

posted date: 2013-05-16 14:01:00


Re: Saving a Base64 string to disk as a binary using PHP#10
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;}

posted date: 2016-04-25 23:31:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.