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