Saving a Base64 string to disk as a binary using PHP

前端 未结 8 1483
无人及你
无人及你 2020-12-24 15:01

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

相关标签:
8条回答
  • 2020-12-24 16:06

    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

    0 讨论(0)
  • 2020-12-24 16:07

    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);
    ?>
    
    0 讨论(0)
提交回复
热议问题