How to save a base64 decoded image in the filesystem using php?

前端 未结 2 851
忘了有多久
忘了有多久 2021-02-04 15:12

I am getting a base64 encoded JPEG string via a POST request to my web service. I want to decode it and save it in the filesystem. How can I achieve this using PHP 5.3. I am

相关标签:
2条回答
  • 2021-02-04 15:42

    If you are sure the image will always be jpg then you can simply use: file_put_contents();

    <?php 
    $decoded=base64_decode($encodedString);
    
    file_put_contents('newImage.JPG',$decoded);
    //leave it to you to randomize the filename.
    ?>
    
    0 讨论(0)
  • 2021-02-04 15:49

    Replacing the blank spaces with + signs is required if the data is derived from canvas.toDataURL() function.

     $encodedString = str_replace(' ','+',$encodedString);
    

    See this question

    It helped a lot in my case.

    0 讨论(0)
提交回复
热议问题