multipart/form-data into array not processing


Basically, I have this form data I'm trying to pass using cURL, here it is hardcoded into boundaries and just sending the request.

$postfields = '--Boundary+0xAbCdEfGbOuNdArY'."\r\n"; $postfields .= 'Content-Disposition: form-data; name="device_timestamp"'."\r\n\r\n"; $postfields .= (time() - (100 * rand(1,6)))."\r\n"; $postfields .= '--Boundary+0xAbCdEfGbOuNdArY'."\r\n"; $postfields .= 'Content-Disposition: form-data; name="photo"; filename="photo"'."\r\n"; $postfields .= 'Content-Type: image/jpeg'."\r\n\r\n"; $postfields .= file_get_contents($path)."\r\n"; $postfields .= '--Boundary+0xAbCdEfGbOuNdArY--'."\r\n"; $result = $this->curl_request('api.com/upload/',$postfields,array( CURLOPT_HTTPHEADER => array( 'Content-type: multipart/form-data; boundary=Boundary+0xAbCdEfGbOuNdArY', 'Content-Length: '.strlen($postfields), 'Expect:' ) ));

How could I pass this data into a function like so?

private function multipart_build_query($fields){ $retval = ''; foreach($fields as $key => $value){ $retval .= "--".$this->boundary."\r\nContent-Disposition: form-data; name=\"$key\"\r\n\r\n$value\r\n"; } $retval .= "--".$this->boundary."--"; return $retval; }

I'm kind of guessing I'd have to modify my multipart_build_query due to the following line : Content-Type: image/jpeg

I tried doing the following

$data_array = array( "device_timestamp" => (time() - (100 * rand(1,6))), "photo" => "@".$path, ); $body = $curl->multipart_build_query($data_array);

yet to no avail


I suggest you make an array like this:

$time = (string) (time() - (100 * rand(1,6))); $photo = file_get_contents($path); $fields = array( array( 'headers' => array( 'Content-Disposition' => 'form-data; name="device_timestamp"', 'Content-Length' => strlen($time) ), 'body' => $time ), array( 'headers' => array( 'Content-Disposition' => 'form-data; name="photo"; filename="photo"', 'Content-Type' => 'image/jpeg', 'Content-Length' => strlen($photo) ), 'body' => $photo ) );

The method can then look like this:

private function multipart_build_query($fields) { $data = ''; foreach ($fields as $field) { // add boundary $data .= '--' . $this->boundary . "\r\n"; // add headers foreach ($field['headers'] as $header => $value) { $data .= $header . ': ' . $value . "\r\n"; } // add blank line $data .= "\r\n"; // add body $data .= $field['body'] . "\r\n"; } // add closing boundary if there where fields if ($data) { $data .= $data .= '--' . $this->boundary . "--\r\n"; } return $data; }

You now have a very generic method which supports any kind of field.


  • send HTTP Get request using Curl in c
  • Sending a POST with PHP doesn't work with curl or file_get_contents, just normal bash CURL
  • libcurl - POST after PUT issue
  • php rest curl authentication header items missing
  • How do I retrieve the visitor's ISP through PHP
  • cURL POST of custom binary data (not form contents)
  • curl_exec not working with Veeva Vault integration into CakePHP
  • PHP not converting JSON using 'json_decode()'
  • curl not working for getting a web page content, why?
  • Zipping the contents of a folders in PHP
  • How to access list of email accounts with cPanel API?
  • Valums Ajax file Upload handle the up. file?
  • Argument 5: cannot convert from 'System.Drawing.Image' to 'string' - calling cla
  • Matplotlib save file as JPEG encoder error
  • Read the values from XML file
  • MVVM: Image Bind Source from FileOpenPicker
  • Save image as is in photo album using swift
  • PHP Listener Script for Paypal Webhooks
  • Can't delete or rename original file after resizing
  • how to upload multiple files in c# windows application
  • How can I enlarge video fullscreen without the affected interface project in as3?
  • How to render a blob on a canvas element?
  • Python urlparse: small issue
  • How to install a .deb file on a jailbroken iphone programmatically?
  • Handling un-mapped Rest path
  • FileReader+canvas image loading problem
  • script to move all files from one location to another location
  • ILMerge & Keep Assembly Name
  • Symfony2: How to get request parameter
  • php design question - will a Helper help here?
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • AngularJs get employee from factory
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • python draw pie shapes with colour filled
  • reshape alternating columns in less time and using less memory
  • Binding checkboxes to object values in AngularJs