Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php http request content raw data enctype=multipart/form-data [duplicate]

I'm currently coding two classes: HttpRequest and HttpResponse. I want to code my own HTTP classes.

I'm encountering an issue with a POST method using a form with enctype=multipart/form-data: I can't get the request contents.

After a long research and searchs, I've found I have to use file_get_contents("php://input") to get the request content. When I test it, I have an empty string var_dump(file_get_contents("php://input")).

I have no access to server/php configuration.

I'm testing with the following code :

<?php
$input = file_get_contents('php://input');
var_dump($input);
?>
<html>
<body>
<form action="./" method="post" enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="text" name="namen" id="nameid" /><br/>
    <input type="file" name="file" id="file"><br>
    <input type="file" name="file2" id="file2"><br>
    <input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

And after submitting, I have this result : string(0) "". It's obvious because php://input doesn't work with enctype=”multipart/form-data” forms.

I'd like to have something like :

-----------------------------19972703421793859182225487886
Content-Disposition: form-data; name="namen"

ds
-----------------------------19972703421793859182225487886
Content-Disposition: form-data; name="file"; filename="toto.xml"
Content-Type: text/xml

<?xml version="1.0" encoding="UTF-8"?>
<Toto></Toto>

-----------------------------19972703421793859182225487886
Content-Disposition: form-data; name="file2"; filename=""
Content-Type: application/octet-stream


-----------------------------19972703421793859182225487886
Content-Disposition: form-data; name="submit"

Submit
-----------------------------19972703421793859182225487886--

Is a way it works for any request method ? Or am I wrong ?

like image 309
RaphyTheGeek Avatar asked Oct 31 '13 13:10

RaphyTheGeek


2 Answers

php://input is not available with enctype="multipart/form-data". http://php.net/manual/en/wrappers.php.php

like image 111
Jérémie Aïssa Nayrand Avatar answered Nov 14 '22 22:11

Jérémie Aïssa Nayrand


I'm going for the obvious one here: why even bother trying to parse this yourself? The data is within your range with the easy use of $_POST[] / $_FILES[] ..

You can still build your own class around those values instead of having your own classes try to accomplish the same data array as the php-compiler already did for you.

In order to give you a valid answer: I don't know of a way to get your php://input back, as it is probably read by the PHP compiler and therefore you are at the end of the stream ( plus: it is read-only so there is probably no way to reset the pointer to the start of the stream ).

like image 41
Stijn van Grinsven Avatar answered Nov 14 '22 23:11

Stijn van Grinsven