title: PHP cURL: upload file from form to remote API?
body: <p>I'm trying to upload a file on a form on my site, and then pass it on to a remote API.   </p>
  
  <p>This is my PHP:</p>
  
  <pre><code>$fields = array(
      'file'=&gt;$_FILES["mediaupload"],
      'username'=&gt;urlencode($_POST["username"]),
      'password'=&gt;urlencode($_POST["password"]),
      'latitude'=&gt;urlencode($_POST["latitude"]),
      'longitude'=&gt;urlencode($_POST["longitude"]),
  );
  $fields_string = http_build_query($fields);
  $url = my_url;
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_POST,count($fields));
  curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $response = curl_exec ($ch);
  </code></pre>
  
  <p>At the moment I keep getting error messages that the file could not be processed properly. The API expects all fields as POST strings except the file, which it expects in binary.</p>
  
  <p>I know it's going to be tough to debug this for you guys without access to the remote API, but am I doing anything <strong>obviously</strong> wrong, or should this work?</p>
  
  <p>Many thanks. </p>