I'm looking for a drop-in include script / class that dissects multipart/form-data
and fills up $_POST
(+raw) and $_FILES
from it. Usually PHP does that itself. But because the automatic handling is insufficient for me and makes php://input
inaccesible[1] I'll probably be using something like this to prevent that:
RewriteRule .* - [E=CONTENT_TYPE:noparsing/for-you-php]
Does not work. Actual solution requires mod_headers andRequestHeader set
...
The extracting procedure might not be that complex. But I'd rather use a well-tested solution. And foremost I would prefer an implementation that uses fgets
for splitting, and mimics the $_FILES
handling closely and efficiently. Finding the end of binary payloads would seem rather tricky to me, in particular when you have to strip off \r\n
but might encounter clients that only send \n
(not allowed, but possible).
I'm certain something like this exists. But I'm having a hard time googling it. Does anyone know an implementation? (PEAR::mimeDecode can be hacked to get sort of working for form-data, but is a memory hog.)
The use case in short: need to preserve the raw field names (including whitespace and special characters), for logging, but can't avoid file uploads always.
For decorative purposes, that's how a POST request looks:
POST / HTTP/1.1
Host: localhost:8000
Content-Length: 17717
Content-Type: multipart/form-data; boundary=----------3wCuBwquE9P7A4OEylndVx
And after a \r\n\r\n
sequence the multipart/ payload follows like this:
------------3wCuBwquE9P7A4OEylndVx
Content-Disposition: form-data; name="_charset_"
windows-1252
------------3wCuBwquE9P7A4OEylndVx
Content-Disposition: form-data; name=" text field \\ 1 \";inject=1"
text1 te twj sakfkl
------------3wCuBwquE9P7A4OEylndVx
Content-Disposition: form-data; name="file"; filename="dial.png"
Content-Type: image/png
IPNG Z @@@MIHDR@@B`@@B;HF@@@-'.e@@@AsRGB@.N\i@@@FbKGD@?@?@? ='S@@@
@@@GtIMEGYAAU,#}BRU@@@YtEXtComment@Created with GIMPWANW@@ @IDATxZl]w|