Skip to content
Advertisement

How to store and retrieve images of type data:image/jpeg;base64?

I’m trying to upload images from the front-end to my PHP server. I’m converting the image into data:image/jpeg;base64 using FileReader() in javascript. Here’s my js code:

JavaScript

I inserted the full “image/jpeg;base64,…” string into my MySQL database (in long text type).

JavaScript

But when I tried displaying the images after retrieving the src-links from the database, the images are either destroyed or not displayed at all. Here’s the code:

JavaScript

How should I properly store and retrieve such image-data in PHP?

Advertisement

Answer

Finally Solved! The issue was that the base64 data string received by the PHP server was missing the + symbol. The + symbols were replaced by white spaces. Even the urldecode() in PHP was not reading the + symbol properly.

So by replacing those white spaces by + worked out!

Front-end:

JavaScript

Back-end:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement