1

I'm trying to retrieve the data from a PUT request.

Currently I have it working with the below code:

parse_str(file_get_contents("php://input"), $parsedArray);

But this seems to only be supported with x-www-url-encoded and not the standard form-data, is there a work around that I've missed?

If it's any addition I have the following headers set on all requests to my API:

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: *");
header("Access-Control-Allow-Methods: OPTIONS, GET, POST, PUT, DELETE");
header("Access-Control-Allow-Headers: Authorization");
header("Content-Type: application/json");

If more info is needed i can provide it, not too sure what else could be effecting this?

Cheers in advance, Jamie

Jamie Street
  • 1,527
  • 3
  • 14
  • 32

3 Answers3

2

I found this, it provides good extra background (got curious myself): PUT vs POST

Community
  • 1
  • 1
poli_g
  • 629
  • 3
  • 15
1

This is not specific to PUT - POST suffers the same problem.

When PHP parses a multipart/form-data request, php://input is no longer available.

This has caused many problems for me in the past!

Unfortunately the only workaround is to have the file's contents be submitted as part of the form-data, possibly base64-encoded (so it doesn't have to urlencode all the binary data). It's a hassle, but sadly that's the only way to handle this particular situation...

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • 1
    I see. I think I may be using `php://input` wrong then. I'm not looking to pass a file, just get form-data into my PHP script for a PUT. Does it make any difference that I'm not dealing with files, and instead just parameters/fields? – Jamie Street Feb 24 '14 at 17:15
  • 1
    @JamieStreet If you're not dealing with files, you generally shouldn't be using `multipart/form-data` over `application/x-www-form-urlencoded` (yes, I know it's been a while, but still there simply hadn't been an answer so far) – Jasper Jul 20 '16 at 19:24
0

There is some issue about it on PHP.

https://bugs.php.net/bug.php?id=55815

On the other hand, as they say over there, PUT is not the proper way to upload a file. It is a discussion about to solve the problem in Symfony Framework or not.

https://github.com/symfony/symfony/pull/10381#issuecomment-36726684

bichotll
  • 469
  • 1
  • 5
  • 8