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
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
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);
?>