1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2012, 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 http://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 ***************************************************************************/
25 #include <curl/curl.h>
26 #include <sys/types.h>
37 * This example shows an FTP upload, with a rename of the file just after
38 * a successful upload.
40 * Example based on source code provided by Erick Nuwendam. Thanks!
43 #define LOCAL_FILE "/tmp/uploadthis.txt"
44 #define UPLOAD_FILE_AS "while-uploading.txt"
45 #define REMOTE_URL "ftp://example.com/" UPLOAD_FILE_AS
46 #define RENAME_FILE_TO "renamed-and-fine.txt"
48 /* NOTE: if you want this example to work on Windows with libcurl as a
49 DLL, you MUST also provide a read callback with CURLOPT_READFUNCTION.
50 Failing to do so will give you a crash since a DLL may not use the
51 variable's memory when passed in to it from an app like this. */
52 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
55 /* in real-world cases, this would probably get this data differently
56 as this fread() stuff is exactly what the library already would do
57 by default internally */
58 size_t retcode = fread(ptr, size, nmemb, stream);
60 nread = (curl_off_t)retcode;
62 fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
63 " bytes from file\n", nread);
72 struct stat file_info;
75 struct curl_slist *headerlist=NULL;
76 static const char buf_1 [] = "RNFR " UPLOAD_FILE_AS;
77 static const char buf_2 [] = "RNTO " RENAME_FILE_TO;
79 /* get the file size of the local file */
80 if(stat(LOCAL_FILE, &file_info)) {
81 printf("Couldnt open '%s': %s\n", LOCAL_FILE, strerror(errno));
84 fsize = (curl_off_t)file_info.st_size;
86 printf("Local file size: %" CURL_FORMAT_CURL_OFF_T " bytes.\n", fsize);
88 /* get a FILE * of the same file */
89 hd_src = fopen(LOCAL_FILE, "rb");
91 /* In windows, this will init the winsock stuff */
92 curl_global_init(CURL_GLOBAL_ALL);
94 /* get a curl handle */
95 curl = curl_easy_init();
97 /* build a list of commands to pass to libcurl */
98 headerlist = curl_slist_append(headerlist, buf_1);
99 headerlist = curl_slist_append(headerlist, buf_2);
101 /* we want to use our own read function */
102 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
104 /* enable uploading */
105 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
108 curl_easy_setopt(curl,CURLOPT_URL, REMOTE_URL);
110 /* pass in that last of FTP commands to run after the transfer */
111 curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
113 /* now specify which file to upload */
114 curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
116 /* Set the size of the file to upload (optional). If you give a *_LARGE
117 option you MUST make sure that the type of the passed-in argument is a
118 curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
119 make sure that to pass in a type 'long' argument. */
120 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
123 /* Now run off and do what you've been told! */
124 res = curl_easy_perform(curl);
125 /* Check for errors */
127 fprintf(stderr, "curl_easy_perform() failed: %s\n",
128 curl_easy_strerror(res));
130 /* clean up the FTP commands list */
131 curl_slist_free_all (headerlist);
134 curl_easy_cleanup(curl);
136 fclose(hd_src); /* close the local file */
138 curl_global_cleanup();