1/6/2018
Posted by 
Curl Post File Example

To POST with a file, add this -F file=@'path/to/data.txt' 2.1 Upload a file $ curl -F file=@'path/to/data.txt' 2.2 Upload multiple files, with extra fields: $ curl -F extraField='abc' -F files=@'path/to/data.txt' -F files=@'path/to/data2.txt' 2.3 Spring REST to accept POST Multipart data.

From: Daniel Stenberg Date: Wed, 21 Apr 2004 08:49:31 +0200 (CEST) On Tue, 20 Apr 2004, Jesse Noller wrote: >We have a 3 GB file, we want to upload via a form POST, for instance: >>curl -i -F path=/home/foo/tmp/3GB.dat -F >uploadedFile=@/home/foo/tmp/3GB. Bakemonogatari Season 1 Sub Indo. dat >>The process runs for awhile after execution, and then linux throws: >Killed The formpost code in libcurl will create the whole post in memory before attempting to post it. This is of course rendering the -F feature useless if you want to post something that is bigger than your amount of available memory. (Yes I want that fixed, but my work load is high already.) >We're assuming that the URL encoding required for a form POST This is a multipart formpost, it doesn't need any URL encoding. It just needs RFC1867-formatting. >The question is this - does curl itself have a method to url encode on disk, >and then pass a pre-encoded file to the server, and secondarily, how do we >avoid this with libcurl? You can avoid this by producing a full pre-formatted multipart chunk on disk and then using the 'standard' POST function with (lib)curl as that can be done from a file/using the plain read callback functionality.

There is however no support in libcurl to produce such a pre-formatted multipart post file, you would need to read up on rfc1867 details and do it yourself. But personally, I consider the fix to make it not produce the full post in memory before sending to be the most sensible action to circumvent this problem. >In python/pycurl's case, it would be a matter of importing httplib and url >encoding the file, but how do you pass libcurl the handle to say 'this file >is already encoded?' Libcurl doesn't encode anything you tell it to post!:-) -- Daniel Stenberg -- -- Dedicated custom curl help for hire: Received on 2004-04-21.

I've used curl for connecting with a web service and upload a file using a similar idea of this post, but it didn't work. The reasons where different to the previous answer, so I will describe my solution; maybe my experience is useful to others. I had two problems. The first one is that my web service didn't like absolute path in the filename. If $file in the OP code has a path, then using: $postdata = array('file[]' =>'@/'.realpath($file)); becomes: Content-Disposition: form-data; name='file[]'; filename='/tmp/iy56ham' and that path generated a BAD REQUEST.

So I had to use chdir, basename and dirname in order to avoid using paths in the $postdata. On the other hand, curl sent an extra header, not shown in the OP: Expect: 100-continue My web service didn't like it (neither twitter WS) and it answered with a: 417 Expectation Failed To avoid curl sending that offending header you can use: curl_setopt( $curlHandler, CURLOPT_HTTPHEADER, array('Expect:') ); so you set an empty Expect header and curl will not overwritten with 100-continue. Action Replay Evo Edition. Hope this help someone.