Saving a Base64 string to disk as a binary using PHP

前端 未结 8 1555
无人及你
无人及你 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

提交回复
热议问题