Posting multipart form data using PHP

@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 :)

echo adrotate_group(4);
$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);

15 thoughts on “Posting multipart form data using PHP

  1. Yoan

    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.

    Reply
  2. Andrew Glowik

    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!

    Reply
  3. Afanasy Kurakin

    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.

    Reply
  4. Jason

    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

    Reply
  5. Albi Patozi

    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

    Reply
      1. Albi Patozi

        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)

        Reply
        1. Claude

          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

          Reply
          1. albi patozi

            Thanks a lot. i have posted the question there but nobody answered. hehe . it doesnt metter now i have found another solution without post .

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.