Skip to content

What are the ways of sending images to my server?

I’m mostly a PHP backend / HTML/CSS frontend developer, so I’ve always sent images to my server from the client using either form enctype: multipart/formdata or through a FormData object when uploading through ajax requests. After that I would receive the file in the $_FILES array. Lately I’ve been working with REST APIs and I’ve been doing some research about sending files. I’m trying to build an app using Flutter, so I need a way to send files to my API. The thing is I don’t know the best way to do it, should I encode images in Base64? Is there any alternative to doing this? How about other types of files? At the time of writing this I’ve noticed that I don’t even know how PHP manages to receive files, does it encode them? Finally my question would be, which are the ways of sending files / images to a REST API? How to encode them if needed? And as a plus is anyone knows how PHP handles the connection between a form and the FILES array it would be quite helpful!



Does the back end support JSON as well as multipart form data, and does it handle base64 encoded data? That’s pretty much up to you, unless the back end is already developed, in which case you will have to find out what it accepts.

Suppose you are coding the back end yourself. The app might read in image data into a buffer, at this point it’s just a sequence of bytes and doesn’t have any encoding. Just plain old binary with a size of X bytes. If you base64 encode that, your backend will have to be prepared for it.

In any case, all the input is read from the standard input, and whether PHP is executed as a server module or as a CGI process, that’s how it reads the data sent from the browser. Usually there is a higher level API to retrieve the data, and that’s what the $_FILES array is all about.

Check out developing an API with PHP using a framework like Symfony, Laravel, CodeIgniter or something else. That makes retrieving whatever the browser sent much more comfortable and less fiddly, the details are taken care of for you.

Then you just have to figure out whether you are receiving a blob of bytes like binary data, or base64 encoded data. If the latter, just decode it and save it on the file system with the appropriate file extension, like jpg, png, etc.

7 People found this is helpful