2

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?

johnnietheblack
  • 13,050
  • 28
  • 95
  • 133

3 Answers3

2

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

webbiedave
  • 48,414
  • 8
  • 88
  • 101
  • urldecode? I remember that if the client sends a POST with content type application/octet-stream, then the POSTDATA are raw bytes, in that case an image, and you would store it as it is, no need to urldecode it... – gd1 May 04 '11 at 22:37
  • I was actually asking. If you remember differently than me, maybe you're right... PS: +1 – gd1 May 04 '11 at 22:38
1

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!

gd1
  • 11,300
  • 7
  • 49
  • 88
1

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

Community
  • 1
  • 1
Alix Axel
  • 151,645
  • 95
  • 393
  • 500