How to handle images sent by a mobile device?

2019-07-16 07:00发布

A peer of mine is developing an iPhone application that will allow users to post images on my site via my API. I am building the part of the API that will accept and process the images.

The mobile developer is sending headers like such:

Content-Disposition: form-data; name="photo_1"; filename="photo_1.jpg"
    Content-Type: application/octet-stream

When looking for the images sent, is it the same method as with normal HTML forms? Should I look for $_FILES?

Or, using PHP, how would I find his image?

3条回答
萌系小妹纸
2楼-- · 2019-07-16 07:16

Doesn't appear it's being sent via a form, i.e., <form enctype=multipart/form-data"> and <input type="file">, so the $_FILES array won't be populated.

You'll probably need to read:

$HTTP_RAW_POST_DATA

or do:

$rawPost = file_get_contents("php://input");

From the manual:

php://input allows you to read raw data from the request body. In case of POST requests, it preferrable to $HTTP_RAW_POST_DATA as it does not depend on special php.ini directives. Moreover, for those cases where $HTTP_RAW_POST_DATA is not populated by default, it is a potentially less memory intensive alternative to activating always_populate_raw_post_data. php://input is not available with enctype="multipart/form-data".

For more info, check out:

http://php.net/manual/en/wrappers.php.php

http://php.net/manual/en/reserved.variables.httprawpostdata.php

查看更多
Deceive 欺骗
3楼-- · 2019-07-16 07:18
疯言疯语
4楼-- · 2019-07-16 07:25

I suppose iOS is sending the whole file as a single block of data in the POSTDATA section of the HTTP request. You can retrieve the whole POSTDATA (not parsed):

<?php
$postdata = file_get_contents("php://input");
?> 

$_FILES is meant for reading files sent with enctype="multipart/form-data" in a proper HTML form. iOS is probably sending a plain old POST containing just a bunch of bytes which represent the file.

Tell me if this solves!

查看更多
登录 后发表回答