1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: ftpupload.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
14 #include <curl/curl.h>
15 #include <sys/types.h>
26 * This example shows an FTP upload, with a rename of the file just after
27 * a successful upload.
29 * Example based on source code provided by Erick Nuwendam. Thanks!
32 #define LOCAL_FILE "/tmp/uploadthis.txt"
33 #define UPLOAD_FILE_AS "while-uploading.txt"
34 #define REMOTE_URL "ftp://localhost/" UPLOAD_FILE_AS
35 #define RENAME_FILE_TO "renamed-and-fine.txt"
37 /* NOTE: if you want this example to work on Windows with libcurl as a
38 DLL, you MUST also provide a read callback with CURLOPT_READFUNCTION.
39 Failing to do so will give you a crash since a DLL may not use the
40 variable's memory when passed in to it from an app like this. */
41 static size_t read_callback(void *ptr
, size_t size
, size_t nmemb
, void *stream
)
43 /* in real-world cases, this would probably get this data differently
44 as this fread() stuff is exactly what the library already would do
45 by default internally */
46 size_t retcode
= fread(ptr
, size
, nmemb
, stream
);
48 fprintf(stderr
, "*** We read %d bytes from file\n", retcode
);
52 int main(int argc
, char **argv
)
57 struct stat file_info
;
59 struct curl_slist
*headerlist
=NULL
;
60 static const char buf_1
[] = "RNFR " UPLOAD_FILE_AS
;
61 static const char buf_2
[] = "RNTO " RENAME_FILE_TO
;
63 /* get the file size of the local file */
64 if(stat(LOCAL_FILE
, &file_info
)) {
65 printf("Couldnt open '%s': %s\n", LOCAL_FILE
, strerror(errno
));
68 printf("Local file size: %ld bytes.\n", file_info
.st_size
);
70 /* get a FILE * of the same file */
71 hd_src
= fopen(LOCAL_FILE
, "rb");
73 /* In windows, this will init the winsock stuff */
74 curl_global_init(CURL_GLOBAL_ALL
);
76 /* get a curl handle */
77 curl
= curl_easy_init();
79 /* build a list of commands to pass to libcurl */
80 headerlist
= curl_slist_append(headerlist
, buf_1
);
81 headerlist
= curl_slist_append(headerlist
, buf_2
);
83 /* we want to use our own read function */
84 curl_easy_setopt(curl
, CURLOPT_READFUNCTION
, read_callback
);
86 /* enable uploading */
87 curl_easy_setopt(curl
, CURLOPT_UPLOAD
, 1L);
90 curl_easy_setopt(curl
,CURLOPT_URL
, REMOTE_URL
);
92 /* pass in that last of FTP commands to run after the transfer */
93 curl_easy_setopt(curl
, CURLOPT_POSTQUOTE
, headerlist
);
95 /* now specify which file to upload */
96 curl_easy_setopt(curl
, CURLOPT_READDATA
, hd_src
);
98 /* Set the size of the file to upload (optional). If you give a *_LARGE
99 option you MUST make sure that the type of the passed-in argument is a
100 curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
101 make sure that to pass in a type 'long' argument. */
102 curl_easy_setopt(curl
, CURLOPT_INFILESIZE_LARGE
,
103 (curl_off_t
)file_info
.st_size
);
105 /* Now run off and do what you've been told! */
106 res
= curl_easy_perform(curl
);
108 /* clean up the FTP commands list */
109 curl_slist_free_all (headerlist
);
112 curl_easy_cleanup(curl
);
114 fclose(hd_src
); /* close the local file */
116 curl_global_cleanup();