cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Uploading a file via HTTP Post

From: Emil Romanus <sdac.bithack_at_gmail.com>
Date: Sat, 30 Jun 2007 21:05:23 +0200

Hi Carter,

2007/6/30, Carter R. Harrison <carterharrison_at_mac.com>:
>
> I've been struggling with this for a while now and I just can't seem to
> make this work... I want to upload a file (in this case a .torrent file) to
> a php web application. I can upload it via CURL on the command line by
> typing the following:
> curl -F upload_file=@filename.torrent http://localhost/index.php
>
> now I'm trying to translate that line into the appropriate calls to
> libcurl. Here's what I have.. The result is that CURL makes the post, but
> my file is never uploaded. Thanks in advance for any help.
>
> //Get a CURL.
> CURL *mCURL;
> mCURL = curl_easy_init();
>
> //Setup CURL to upload the torrent.
> struct curl_httppost *post=NULL;
> struct curl_httppost *last=NULL;
> char *filename="filename.torrent";
> curl_formadd(&post, &last, CURLFORM_COPYNAME, "upload_file",
> CURLFORM_COPYCONTENTS, filename, CURLFORM_END);
>

To set the "upload_file" field to the actual content of the file "
filename.torrent", I believe you are supposed to use CURLFORM_FILECONTENT
instead of CURLFORM_COPYCONTENTS, replace the above line with:
curl_formadd(&post, &last, CURLFORM_COPYNAME, "upload_file",
CURLFORM_FILECONTENT, filename, CURLFORM_END);

Now I'm not sure how your server-side upload script works, and if the above
mentioned does not work, you should try the following:
curl_formadd(&post, &last, CURLFORM_COPYNAME, "upload_file", CURLFORM_FILE,
filename, CURLFORM_END);

Also, in this case since you are using curl_easy_perform() which doesn't
return until it's done, you may use CURLFORM_PTRNAME instead. On large
buffers this would avoid having multiple copies in memory, thus
theoretically speed up your application (however, in practice you are
probably restricted by network speed anyway).

curl_easy_setopt(mCURL, CURLOPT_POSTFIELDS, post);
> curl_easy_setopt(mCURL, CURLOPT_POST, 1);
> curl_easy_setopt(mCURL, CURLOPT_URL, "http://localhost/index.php");
>
> //Post away
> curl_easy_perform(mCURL);
>
>
>
>
Received on 2007-06-30