1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 * FTP upload a file from memory
28 #include <curl/curl.h>
30 static const char data[]=
31 "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
32 "Nam rhoncus odio id venenatis volutpat. Vestibulum dapibus "
33 "bibendum ullamcorper. Maecenas finibus elit augue, vel "
34 "condimentum odio maximus nec. In hac habitasse platea dictumst. "
35 "Vestibulum vel dolor et turpis rutrum finibus ac at nulla. "
36 "Vivamus nec neque ac elit blandit pretium vitae maximus ipsum. "
37 "Quisque sodales magna vel erat auctor, sed pellentesque nisi "
38 "rhoncus. Donec vehicula maximus pretium. Aliquam eu tincidunt "
46 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
48 struct WriteThis *upload = (struct WriteThis *)userp;
49 size_t max = size*nmemb;
54 if(upload->sizeleft) {
56 if(copylen > upload->sizeleft)
57 copylen = upload->sizeleft;
58 memcpy(ptr, upload->readptr, copylen);
59 upload->readptr += copylen;
60 upload->sizeleft -= copylen;
64 return 0; /* no more data left to deliver */
72 struct WriteThis upload;
74 upload.readptr = data;
75 upload.sizeleft = strlen(data);
77 /* In windows, this will init the winsock stuff */
78 res = curl_global_init(CURL_GLOBAL_DEFAULT);
79 /* Check for errors */
81 fprintf(stderr, "curl_global_init() failed: %s\n",
82 curl_easy_strerror(res));
86 /* get a curl handle */
87 curl = curl_easy_init();
89 /* First set the URL, the target file */
90 curl_easy_setopt(curl, CURLOPT_URL,
91 "ftp://example.com/path/to/upload/file");
93 /* User and password for the FTP login */
94 curl_easy_setopt(curl, CURLOPT_USERPWD, "login:secret");
96 /* Now specify we want to UPLOAD data */
97 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
99 /* we want to use our own read function */
100 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
102 /* pointer to pass to our read function */
103 curl_easy_setopt(curl, CURLOPT_READDATA, &upload);
105 /* get verbose debug output please */
106 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
108 /* Set the expected upload size. */
109 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
110 (curl_off_t)upload.sizeleft);
112 /* Perform the request, res will get the return code */
113 res = curl_easy_perform(curl);
114 /* Check for errors */
116 fprintf(stderr, "curl_easy_perform() failed: %s\n",
117 curl_easy_strerror(res));
120 curl_easy_cleanup(curl);
122 curl_global_cleanup();