1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: httpput.c,v 1.5 2004/01/05 22:29:30 bagder Exp $
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.
24 size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
28 /* in real-world cases, this would probably get this data differently
29 as this fread() stuff is exactly what the library already would do
30 by default internally */
31 retcode = fread(ptr, size, nmemb, stream);
33 fprintf(stderr, "*** We read %d bytes from file\n", retcode);
38 int main(int argc, char **argv)
45 struct stat file_info;
56 /* get the file size of the local file */
57 hd = open(file, O_RDONLY) ;
58 fstat(hd, &file_info);
61 /* get a FILE * of the same file, could also be made with
62 fdopen() from the previous descriptor, but hey this is just
64 hd_src = fopen(file, "rb");
66 /* In windows, this will init the winsock stuff */
67 curl_global_init(CURL_GLOBAL_ALL);
69 /* get a curl handle */
70 curl = curl_easy_init();
72 /* we want to use our own read function */
73 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
75 /* enable uploading */
76 curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ;
79 curl_easy_setopt(curl, CURLOPT_PUT, TRUE);
81 /* specify target URL, and note that this URL should include a file
82 name, not only a directory */
83 curl_easy_setopt(curl,CURLOPT_URL, url);
85 /* now specify which file to upload */
86 curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
88 /* and give the size of the upload */
89 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, file_info.st_size);
91 /* Now run off and do what you've been told! */
92 res = curl_easy_perform(curl);
95 curl_easy_cleanup(curl);
97 fclose(hd_src); /* close the local file */
99 curl_global_cleanup();