CURLOPT_UPLOAD.3 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. .\" generated by cd2nroff 0.1 from CURLOPT_UPLOAD.md
  2. .TH CURLOPT_UPLOAD 3 "2025-01-17" libcurl
  3. .SH NAME
  4. CURLOPT_UPLOAD \- data upload
  5. .SH SYNOPSIS
  6. .nf
  7. #include <curl/curl.h>
  8. CURLcode curl_easy_setopt(CURL *handle, CURLOPT_UPLOAD, long upload);
  9. .fi
  10. .SH DESCRIPTION
  11. The long parameter \fIupload\fP set to 1 tells the library to prepare for and
  12. perform an upload. The \fICURLOPT_READDATA(3)\fP and \fICURLOPT_INFILESIZE(3)\fP or
  13. \fICURLOPT_INFILESIZE_LARGE(3)\fP options are also interesting for uploads. If the
  14. protocol is HTTP, uploading means using the PUT request unless you tell
  15. libcurl otherwise.
  16. Using PUT with HTTP 1.1 implies the use of a "Expect: 100\-continue" header.
  17. You can disable this header with \fICURLOPT_HTTPHEADER(3)\fP as usual.
  18. If you use PUT to an HTTP 1.1 server, you can upload data without knowing the
  19. size before starting the transfer. The library enables this by adding a header
  20. \&"Transfer\-Encoding: chunked". With HTTP 1.0 or if you prefer not to use
  21. chunked transfer, you must specify the size of the data with
  22. \fICURLOPT_INFILESIZE(3)\fP or \fICURLOPT_INFILESIZE_LARGE(3)\fP.
  23. .SH DEFAULT
  24. 0
  25. .SH PROTOCOLS
  26. This functionality affects all supported protocols
  27. .SH EXAMPLE
  28. .nf
  29. static size_t read_cb(char *ptr, size_t size, size_t nmemb, void *userdata)
  30. {
  31. FILE *src = userdata;
  32. /* copy as much data as possible into the 'ptr' buffer, but no more than
  33. 'size' * 'nmemb' bytes */
  34. size_t retcode = fread(ptr, size, nmemb, src);
  35. return retcode;
  36. }
  37. int main(void)
  38. {
  39. CURL *curl = curl_easy_init();
  40. if(curl) {
  41. FILE *src = fopen("local-file", "r");
  42. curl_off_t fsize; /* set this to the size of the input file */
  43. /* we want to use our own read function */
  44. curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_cb);
  45. /* enable uploading */
  46. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  47. /* specify target */
  48. curl_easy_setopt(curl, CURLOPT_URL, "ftp://example.com/dir/to/newfile");
  49. /* now specify which pointer to pass to our callback */
  50. curl_easy_setopt(curl, CURLOPT_READDATA, src);
  51. /* Set the size of the file to upload */
  52. curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)fsize);
  53. /* Now run off and do what you have been told! */
  54. curl_easy_perform(curl);
  55. }
  56. }
  57. .fi
  58. .SH AVAILABILITY
  59. Added in curl 7.1
  60. .SH RETURN VALUE
  61. Returns CURLE_OK
  62. .SH SEE ALSO
  63. .BR CURLOPT_INFILESIZE_LARGE (3),
  64. .BR CURLOPT_PUT (3),
  65. .BR CURLOPT_READFUNCTION (3)