1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
12 #ifdef HAVE_SYS_SOCKET_H
13 #include <sys/socket.h>
15 #ifdef HAVE_SYS_TYPES_H
16 #include <sys/types.h>
18 #ifdef HAVE_SYS_STAT_H
32 * This example shows an FTP upload, with a rename of the file just after
33 * a successful upload.
35 * Example based on source code provided by Erick Nuwendam. Thanks!
41 CURLcode res = CURLE_OK;
44 struct_stat file_info;
45 struct curl_slist *hl;
48 struct curl_slist *headerlist=NULL;
49 const char *buf_1 = "RNFR 505";
50 const char *buf_2 = "RNTO 505-forreal";
53 fprintf(stderr, "Usage: <url> <file-to-upload>\n");
57 hd_src = fopen(libtest_arg2, "rb");
60 fprintf(stderr, "fopen() failed with error: %d %s\n",
61 error, strerror(error));
62 fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
63 return -2; /* if this happens things are major weird */
66 /* get the file size of the local file */
67 hd = fstat(fileno(hd_src), &file_info);
69 /* can't open file, bail out */
71 fprintf(stderr, "fstat() failed with error: %d %s\n",
72 error, strerror(error));
73 fprintf(stderr, "ERROR: cannot open file %s\n", libtest_arg2);
78 if(! file_info.st_size) {
79 fprintf(stderr, "ERROR: file %s has zero size!\n", libtest_arg2);
84 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
85 fprintf(stderr, "curl_global_init() failed\n");
87 return TEST_ERR_MAJOR_BAD;
90 /* get a curl handle */
91 if ((curl = curl_easy_init()) == NULL) {
92 fprintf(stderr, "curl_easy_init() failed\n");
93 curl_global_cleanup();
95 return TEST_ERR_MAJOR_BAD;
98 /* build a list of commands to pass to libcurl */
100 if ((hl = curl_slist_append(headerlist, buf_1)) == NULL) {
101 fprintf(stderr, "curl_slist_append() failed\n");
102 curl_easy_cleanup(curl);
103 curl_global_cleanup();
105 return TEST_ERR_MAJOR_BAD;
107 if ((headerlist = curl_slist_append(hl, buf_2)) == NULL) {
108 fprintf(stderr, "curl_slist_append() failed\n");
109 curl_slist_free_all(hl);
110 curl_easy_cleanup(curl);
111 curl_global_cleanup();
113 return TEST_ERR_MAJOR_BAD;
117 /* enable uploading */
118 test_setopt(curl, CURLOPT_UPLOAD, 1L);
121 test_setopt(curl, CURLOPT_VERBOSE, 1L);
124 test_setopt(curl,CURLOPT_URL, URL);
126 /* pass in that last of FTP commands to run after the transfer */
127 test_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
129 /* now specify which file to upload */
130 test_setopt(curl, CURLOPT_INFILE, hd_src);
132 /* and give the size of the upload (optional) */
133 test_setopt(curl, CURLOPT_INFILESIZE_LARGE,
134 (curl_off_t)file_info.st_size);
136 /* Now run off and do what you've been told! */
137 res = curl_easy_perform(curl);
141 /* clean up the FTP commands list */
142 curl_slist_free_all(headerlist);
144 /* close the local file */
147 curl_easy_cleanup(curl);
148 curl_global_cleanup();