Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Should be possible: here's a form, posted through a browser (irrelevant fields omitted):</p> <pre><code>POST http://host.example.com/somewhere HTTP/1.1 Content-Type: multipart/form-data; boundary=---------------------------7da16b2e4026c Content-Length: 105732 -----------------------------7da16b2e4026c Content-Disposition: form-data; name="NewFile"; filename="test.jpg" Content-Type: image/jpeg (...raw JPEG data here...) -----------------------------7da16b2e4026c Content-Disposition: form-data; name="otherformfield" content of otherformfield is this text -----------------------------7da16b2e4026c-- </code></pre> <p>So, if we build the POST body ourselves and set an extra header or two, we should be able to simulate this:</p> <pre><code>// form field separator $delimiter = '-------------' . uniqid(); // file upload fields: name =&gt; array(type=&gt;'mime/type',content=&gt;'raw data') $fileFields = array( 'file1' =&gt; array( 'type' =&gt; 'text/plain', 'content' =&gt; '...your raw file content goes here...' ), /* ... */ ); // all other fields (not file upload): name =&gt; value $postFields = array( 'otherformfield' =&gt; 'content of otherformfield is this text', /* ... */ ); $data = ''; // populate normal fields first (simpler) foreach ($postFields as $name =&gt; $content) { $data .= "--" . $delimiter . "\r\n"; $data .= 'Content-Disposition: form-data; name="' . $name . '"'; // note: double endline $data .= "\r\n\r\n"; } // populate file fields foreach ($fileFields as $name =&gt; $file) { $data .= "--" . $delimiter . "\r\n"; // "filename" attribute is not essential; server-side scripts may use it $data .= 'Content-Disposition: form-data; name="' . $name . '";' . ' filename="' . $name . '"' . "\r\n"; // this is, again, informative only; good practice to include though $data .= 'Content-Type: ' . $file['type'] . "\r\n"; // this endline must be here to indicate end of headers $data .= "\r\n"; // the file itself (note: there's no encoding of any kind) $data .= $file['content'] . "\r\n"; } // last delimiter $data .= "--" . $delimiter . "--\r\n"; $handle = curl_init($url); curl_setopt($handle, CURLOPT_POST, true); curl_setopt($handle, CURLOPT_HTTPHEADER , array( 'Content-Type: multipart/form-data; boundary=' . $delimiter, 'Content-Length: ' . strlen($data))); curl_setopt($handle, CURLOPT_POSTFIELDS, $data); curl_exec($handle); </code></pre> <p>This way, we're doing all the heavy lifting ourselves, and trusting cURL not to mangle it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload