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