@shvi asked me for this code over Twitter, I though it would a good idea to share it here.
The following code is used to post two different fields, a simple text data named “somedata” and a file named “somefile”.
Hope it helps :)
$destination = "http://yourdomain.com/yoururl"; $eol = "\r\n"; $data = ''; $mime_boundary=md5(time()); $data .= '--' . $mime_boundary . $eol; $data .= 'Content-Disposition: form-data; name="somedata"' . $eol . $eol; $data .= "Some Data" . $eol; $data .= '--' . $mime_boundary . $eol; $data .= 'Content-Disposition: form-data; name="somefile"; filename="filename.ext"' . $eol; $data .= 'Content-Type: text/plain' . $eol; $data .= 'Content-Transfer-Encoding: base64' . $eol . $eol; $data .= chunk_split(base64_encode("Some file content")) . $eol; $data .= "--" . $mime_boundary . "--" . $eol . $eol; // finish with two eol's!! $params = array('http' => array( 'method' => 'POST', 'header' => 'Content-Type: multipart/form-data; boundary=' . $mime_boundary . $eol, 'content' => $data )); $ctx = stream_context_create($params); $response = @file_get_contents($destination, FILE_TEXT, $ctx);



Wasn’t he looking for a SOAP client that supports that lovely spec: http://www.w3.org/TR/SOAP-attachments and the offical SOAP implementation of PHP doesn’t support it: http://stackoverflow.com/questions/1122292/php-soap-client-that-understands-multi-part-messages
He sits on the other side of the POST.
It’s not a big deal to add this imho, http://developer.jelix.org/ticket/656#comment:1
Cheers,
P.S.: I’m sure you would enjoy using the <<< notation instead of doing so many repetitive concatenations.
I didn’t knew this heredoc notation, thanks for the hint :)
Thank you much for this exposition of code. I was at an impasse with PHP/curl using GlassFish/Quercus with a multipart/form-data post. Using your example to build my own form unwwedged me!
Thanks for the great snippet, it still helps!
Please note, that semicolon after $params is missing, plus in my case, when handling upload with php I’ve removed base64 encoding because php couldn’t understand it and kept data encoded.
Dude! You’re the one and only salvation through this nightmare of mine. I struggled hectically trying to find a way to parse a normal text field with my file upload through a multipart/form-data form
Thanks a stash
Jason