1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
15 #include <curl/curl.h>
18 * This example shows a HTTP PUT operation. PUTs a file given as a command
19 * line argument to the URL also given on the command line.
21 * This example also uses its own read callback.
23 * Here's an article on how to setup a PUT handler for Apache:
24 * http://www.apacheweek.com/features/put
27 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
31 /* in real-world cases, this would probably get this data differently
32 as this fread() stuff is exactly what the library already would do
33 by default internally */
34 retcode = fread(ptr, size, nmemb, stream);
36 fprintf(stderr, "*** We read %d bytes from file\n", retcode);
41 int main(int argc, char **argv)
47 struct stat file_info;
58 /* get the file size of the local file */
59 hd = open(file, O_RDONLY) ;
60 fstat(hd, &file_info);
63 /* get a FILE * of the same file, could also be made with
64 fdopen() from the previous descriptor, but hey this is just
66 hd_src = fopen(file, "rb");
68 /* In windows, this will init the winsock stuff */
69 curl_global_init(CURL_GLOBAL_ALL);
71 /* get a curl handle */
72 curl = curl_easy_init();
74 /* we want to use our own read function */
75 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
77 /* enable uploading */
78 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
81 curl_easy_setopt(curl, CURLOPT_PUT, 1L);
83 /* specify target URL, and note that this URL should include a file
84 name, not only a directory */
85 curl_easy_setopt(curl, CURLOPT_URL, url);
87 /* now specify which file to upload */
88 curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
90 /* provide the size of the upload, we specicially typecast the value
91 to curl_off_t since we must be sure to use the correct data size */
92 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
93 (curl_off_t)file_info.st_size);
95 /* Now run off and do what you've been told! */
96 res = curl_easy_perform(curl);
99 curl_easy_cleanup(curl);
101 fclose(hd_src); /* close the local file */
103 curl_global_cleanup();