1--- 2c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 3SPDX-License-Identifier: curl 4Title: CURLOPT_UPLOAD 5Section: 3 6Source: libcurl 7See-also: 8 - CURLOPT_INFILESIZE_LARGE (3) 9 - CURLOPT_PUT (3) 10 - CURLOPT_READFUNCTION (3) 11Protocol: 12 - All 13Added-in: 7.1 14--- 15 16# NAME 17 18CURLOPT_UPLOAD - data upload 19 20# SYNOPSIS 21 22~~~c 23#include <curl/curl.h> 24 25CURLcode curl_easy_setopt(CURL *handle, CURLOPT_UPLOAD, long upload); 26~~~ 27 28# DESCRIPTION 29 30The long parameter *upload* set to 1 tells the library to prepare for and 31perform an upload. The CURLOPT_READDATA(3) and CURLOPT_INFILESIZE(3) or 32CURLOPT_INFILESIZE_LARGE(3) options are also interesting for uploads. If the 33protocol is HTTP, uploading means using the PUT request unless you tell 34libcurl otherwise. 35 36Using PUT with HTTP 1.1 implies the use of a "Expect: 100-continue" header. 37You can disable this header with CURLOPT_HTTPHEADER(3) as usual. 38 39If you use PUT to an HTTP 1.1 server, you can upload data without knowing the 40size before starting the transfer. The library enables this by adding a header 41"Transfer-Encoding: chunked". With HTTP 1.0 or if you prefer not to use 42chunked transfer, you must specify the size of the data with 43CURLOPT_INFILESIZE(3) or CURLOPT_INFILESIZE_LARGE(3). 44 45# DEFAULT 46 470 48 49# %PROTOCOLS% 50 51# EXAMPLE 52 53~~~c 54static size_t read_cb(char *ptr, size_t size, size_t nmemb, void *userdata) 55{ 56 FILE *src = userdata; 57 /* copy as much data as possible into the 'ptr' buffer, but no more than 58 'size' * 'nmemb' bytes */ 59 size_t retcode = fread(ptr, size, nmemb, src); 60 61 return retcode; 62} 63 64int main(void) 65{ 66 CURL *curl = curl_easy_init(); 67 if(curl) { 68 FILE *src = fopen("local-file", "r"); 69 curl_off_t fsize; /* set this to the size of the input file */ 70 71 /* we want to use our own read function */ 72 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_cb); 73 74 /* enable uploading */ 75 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); 76 77 /* specify target */ 78 curl_easy_setopt(curl, CURLOPT_URL, "ftp://example.com/dir/to/newfile"); 79 80 /* now specify which pointer to pass to our callback */ 81 curl_easy_setopt(curl, CURLOPT_READDATA, src); 82 83 /* Set the size of the file to upload */ 84 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)fsize); 85 86 /* Now run off and do what you have been told */ 87 curl_easy_perform(curl); 88 } 89} 90~~~ 91 92# %AVAILABILITY% 93 94# RETURN VALUE 95 96Returns CURLE_OK 97