1

I've developed a REST API in PHP which gets the POST request data from iPhone app.

I'm getting this POST request data in JSON format. I decode it and use it in PHP. Until here everything works fine.

My issue is regarding the data of uploaded file through iPhone app. How to access this uploaded file data in PHP?

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
PHPLover
  • 1
  • 51
  • 158
  • 311
  • @Rob:enctype is actually an HTML attribute which is necessary in
    tag when to upload a file to the server. For more information visit this link : http://www.w3schools.com/tags/att_form_enctype.asp Yes I'm familiar with usage of $_FILES array but my question is when the file is uploaded using iPhone/iOS do I get the uploaded file data in same $_FILES array as that of file uploaded using HTML form? That's my question can I get the $_FILES data in case of file uploaded from iPhone or is there any way around to access the uploaded file data in PHP?
    – PHPLover Feb 16 '15 at 05:29
  • @Rob : First of all let me clear you that I'm a PHP developer and not a iOS developer. So how to send the multipart request in iOS is not my job. The iOS developers are doing that job. My only concern is do I get the $_FILES array in PHP same as of HTML? – PHPLover Feb 16 '15 at 05:44
  • @Rob:Thanks for the in detail explanation. If you could post this as an answer in a more proper manner it would be true justice to this question. I'll upvote and accept it. – PHPLover Feb 16 '15 at 07:26

1 Answers1

1

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.

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044