View Full Version : file upload form in c++

10-17-2008, 12:11 PM
I know that to duplicate

<form action="./test.php" method="post">
<input type="hidden" name="field" value="myval">
<input type="submit" name="subby" value="didsubmit">

you send the command

POST /test.php field=myval&subby=didsubmit HTTP/1.1

But I don't know how to mimic file upload forms like

<form action="./test.php" method="post">
<input type="file" name="myupload">
<input type="submit">

How do I do this?

10-17-2008, 01:13 PM
You send this command? That's not correct HTTP, actually. If you do

POST /test.php?field=myval&subby=didsubmit HTTP/1.1
then you have at least a syntactically correct request-line, but it's still not a valid HTTP/1.1 request. Also, the parameters are encoded as GET parameters, not POST parameters, which can and will confuse scripts that make the distinction.

POST /test.php?field=myval&subby=didsubmit HTTP/1.1
Host: www.example.com
This is the very least you have to send for it to be valid HTTP/1.1. This doesn't solve the GET vs PUT variable problem, though. For that, you have to move the parameters to the entity body.

POST /test.php HTTP/1.1
Host: www.example.com
Content-type: application/x-www-form-urlencoded
Content-length: 27


Now, file upload is even more complicated. It starts with your form suffering from incompatibility problems. I don't know if any browsers actually submit data in such a form. I know for sure that the file upload won't work with PHP. If you want a file upload, then you have to give the form the enctype="multipart/form-data". Otherwise, you'll have problems.

Now, I'm not going to type up how to encode such a request. You can find an example in the HTML 4.01 spec (http://www.w3.org/TR/html401/interact/forms.html#h- Another valuable resource is the HTTP/1.1 spec (http://www.w3.org/Protocols/rfc2616/rfc2616.html).