@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 :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
$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 know 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
Hey For the solution Thanks it was great, but i have a question : WHY DOESNT IT WORK ONLINE BUT ONLY ON LOCALHOST ?! what shold i try to change ? thanks in advice
Albi, not sure about your particular circumstances but some PHP options must be enabled for
file_get_contents
to actually work with URLs.Make sure to check the doc here: http://php.net/manual/en/function.file-get-contents.php
hmm that seems that file_get_contents does not send the $ctx- context. maybe its problem of the php version. have you ever tested it in php 5.2.* ? (by the way, im vary thankful for your reply)
nope, tested on 5.3 only, but there’s no indication it shouldn’t be working with 5.2
you should be posting your code on StackOverflow and post the link here
Thanks a lot. i have posted the question there but nobody answered. hehe . it doesnt metter now i have found another solution without post .
Thanks much! Worked great. Beware to set Content-Type and Content-Transfer-Encoding appropriately for the type of file being uploaded.
Please give me your script with example data.
Not sure what you mean Amin, the script is in the article and you can send any file
Thanks. It helped me.
Thank you!
Your code helped me with posting file to a REST repo service