How to handle images sent by a mobile device?

梦想与她 提交于 2019-12-24 01:44:58

问题


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?


回答1:


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




回答2:


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!




回答3:


See these answers I gave to similar questions (processing uploads from php://input):

  • userland multipart/form-data handler and also
  • How to validate if uploaded file is an image? [file sent via HTML5's File API, received via php://input]


来源:https://stackoverflow.com/questions/5890574/how-to-handle-images-sent-by-a-mobile-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!