Home » Php » php http request content raw data enctype=multipart/form-data

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

Posted by: admin July 12, 2020 Leave a comment


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 :

$input = file_get_contents('php://input');
<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">

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 :

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

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

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

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

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


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

How to&Answers:

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 ).


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