added comment for windows people about READFUNCTION being needed
[platform/upstream/curl.git] / docs / examples / ftpupload.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * $Id$
9  */
10
11 #include <stdio.h>
12
13 #include <curl/curl.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17
18 /*
19  * This example shows an FTP upload, with a rename of the file just after
20  * a successful upload.
21  *
22  * Example based on source code provided by Erick Nuwendam. Thanks!
23  */
24
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"
29
30 int main(int argc, char **argv)
31 {
32   CURL *curl;
33   CURLcode res;
34   FILE *ftpfile;
35   FILE * hd_src ;
36   int hd ;
37   struct stat file_info;
38
39   struct curl_slist *headerlist=NULL;
40   char buf_1 [] = "RNFR " UPLOAD_FILE_AS;
41   char buf_2 [] = "RNTO " RENAME_FILE_TO;
42
43   /* get the file size of the local file */
44   hd = open(LOCAL_FILE, O_RDONLY) ;
45   fstat(hd, &file_info);
46   close(hd) ;
47
48   /* get a FILE * of the same file, could also be made with
49      fdopen() from the previous descriptor, but hey this is just
50      an example! */
51   hd_src = fopen(LOCAL_FILE, "rb");
52
53   /* In windows, this will init the winsock stuff */
54   curl_global_init(CURL_GLOBAL_ALL);
55
56   /* get a curl handle */
57   curl = curl_easy_init();
58   if(curl) {
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);
62
63     /* enable uploading */
64     curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ;
65
66     /* specify target */
67     curl_easy_setopt(curl,CURLOPT_URL, REMOTE_URL);
68
69     /* pass in that last of FTP commands to run after the transfer */
70     curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
71
72     /* now specify which file to upload */
73     curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
74
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
79        like this. */
80
81     /* and give the size of the upload (optional) */
82     curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, file_info.st_size);
83
84     /* Now run off and do what you've been told! */
85     res = curl_easy_perform(curl);
86
87     /* clean up the FTP commands list */
88     curl_slist_free_all (headerlist);
89
90     /* always cleanup */
91     curl_easy_cleanup(curl);
92   }
93   fclose(hd_src); /* close the local file */
94
95   curl_global_cleanup();
96   return 0;
97 }