You say:
If the file is uploaded from HTML form with enctype="multipart/form-data"
and method=POST
I get data of uploaded file in $_FILES
array.
In iOS, you can create a multipart/form-data
request (see these Stack Overflow answers to see how this is done in Objective-C and Swift, respectively). If you do that, then no change is necessary to the PHP code, and the uploaded file will be accessible via PHP's $_FILES
variable, exactly like it is from the HTML form.
But earlier you said:
I'm getting this POST request data in JSON format.
Above, I pointed out that you could just create a request with a Content-Type
of multipart/form-data
from the iOS code which would make the file available via PHP's $_FILES
variable. But that means, though, that the request would not be JSON.
If you want your web service to employ JSON, only, then the iOS code would instead create a request with a Content-Type
of application/json
. And the body of that request would not be multipart request, but rather just plain JSON. And, if you did that, then the PHP code could not use $_FILES
, but rather would json_decode
the response and parse the data out of that. Worse, you cannot include arbitrary binary data in a JSON element value, so the iOS code would have to convert the binary data to text before putting it into the JSON when the request was created (e.g. via base-64) and then the PHP code would grab the base 64 encoded value, then decode it before saving it.
Frankly, that is a lot more work (and the request will be 33% larger as a result of the base-64 encoding process), so I'd be inclined to have the file upload use the multipart/form-data
outlined earlier rather than creating a JSON-based file upload process.