title: How to send a PUT request with a file and an array of data with PHP
body: <p>I've set up a REST service and client in PHP and I'm having a bit of trouble with PUT. </p>
  
  <p>Here's my situation:
  I'm coding a REST resource that should accept an array of data and an image. The REST resource should update an existing record, so I'm using PUT. I'm sending the data with a PHP curl client I wrote. So - pretty much the same situation as if you were sending a HTML multipart form to a PHP script that does a file upload and accepts some additional POST fields - except with PUT and PHP curl..</p>
  
  <p>Up 'till now I've been sending the PUT request something like this (pseudo code):</p>
  
  <pre><code>$a_some_data = array('name' =&gt; 'test', 'user_id' =&gt; 4);
  $body = http_build_query($a_data);
  
  $fh = fopen('php://memory', 'rw');
  fwrite($body);
  rewind($fh);
  
  $ch = curl_init();
  curl_setopt_array($ch, array(
    CURLOPT_URL =&gt; 'http://myapi/resource/someid',
    CURLOPT_PUT =&gt; TRUE,
    CURLOPT_INFILE =&gt; $fh,
    CURLOPT_INFILESIZE =&gt; strlen($body)
    ));
  curl_exec($ch);
  </code></pre>
  
  <p>and reading the data on the server like so:</p>
  
  <pre><code>parse_str(file_get_contents('php://input'), $put_data);
  </code></pre>
  
  <p>..which works just fine.</p>
  
  <p>So now I would like to add a (binary) file into the mix.
  - How would I implement this on the client side?
  - How would I deal with the file on the server?</p>
  
  <p>For a test I set up a HTML form with a file input, copied the raw multipart/form-data request it sends, and tried sending that data as a file with curl in a PUT request. That kind of works, but I would have to parse the raw data on the server manually, which I'm not sure is the best idea. Alternatively, I guess I could send the file as the body of the PUT request, and add the other parameters in the URL as a query string - but I guess that kind of defies the point of a PUT REST resource..</p>
  
  <p>Please share your thoughts on this.
  Thanks!</p>