1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
13 #include <curl/curl.h>
14 #include <sys/types.h>
19 * This example shows an FTP upload, with a rename of the file just after
20 * a successful upload.
22 * Example based on source code provided by Erick Nuwendam. Thanks!
25 #define LOCAL_FILE "/tmp/uploadthis.txt"
26 #define UPLOAD_FILE_AS "while-uploading.txt"
27 #define REMOTE_URL "ftp://localhost/" UPLOAD_FILE_AS
28 #define RENAME_FILE_TO "renamed-and-fine.txt"
30 int main(int argc, char **argv)
37 struct stat file_info;
39 struct curl_slist *headerlist=NULL;
40 char buf_1 [] = "RNFR " UPLOAD_FILE_AS;
41 char buf_2 [] = "RNTO " RENAME_FILE_TO;
43 /* get the file size of the local file */
44 hd = open(LOCAL_FILE, O_RDONLY) ;
45 fstat(hd, &file_info);
48 /* get a FILE * of the same file, could also be made with
49 fdopen() from the previous descriptor, but hey this is just
51 hd_src = fopen(LOCAL_FILE, "rb");
53 /* In windows, this will init the winsock stuff */
54 curl_global_init(CURL_GLOBAL_ALL);
56 /* get a curl handle */
57 curl = curl_easy_init();
59 /* build a list of commands to pass to libcurl */
60 headerlist = curl_slist_append(headerlist, buf_1);
61 headerlist = curl_slist_append(headerlist, buf_2);
63 /* enable uploading */
64 curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ;
67 curl_easy_setopt(curl,CURLOPT_URL, REMOTE_URL);
69 /* pass in that last of FTP commands to run after the transfer */
70 curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
72 /* now specify which file to upload */
73 curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
75 /* NOTE: if you want this example to work on Windows with libcurl as a
76 DLL, you MUST also provide a read callback with
77 CURLOPT_READFUNCTION. Failing to do so will give you a crash since a
78 DLL may not use the variable's memory when passed in to it from an app
81 /* and give the size of the upload (optional) */
82 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, file_info.st_size);
84 /* Now run off and do what you've been told! */
85 res = curl_easy_perform(curl);
87 /* clean up the FTP commands list */
88 curl_slist_free_all (headerlist);
91 curl_easy_cleanup(curl);
93 fclose(hd_src); /* close the local file */
95 curl_global_cleanup();