1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: httpput.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
16 #include <curl/curl.h>
19 * This example shows a HTTP PUT operation. PUTs a file given as a command
20 * line argument to the URL also given on the command line.
22 * This example also uses its own read callback.
24 * Here's an article on how to setup a PUT handler for Apache:
25 * http://www.apacheweek.com/features/put
28 static size_t read_callback(void *ptr
, size_t size
, size_t nmemb
, void *stream
)
32 /* in real-world cases, this would probably get this data differently
33 as this fread() stuff is exactly what the library already would do
34 by default internally */
35 retcode
= fread(ptr
, size
, nmemb
, stream
);
37 fprintf(stderr
, "*** We read %d bytes from file\n", retcode
);
42 int main(int argc
, char **argv
)
48 struct stat file_info
;
59 /* get the file size of the local file */
60 hd
= open(file
, O_RDONLY
) ;
61 fstat(hd
, &file_info
);
64 /* get a FILE * of the same file, could also be made with
65 fdopen() from the previous descriptor, but hey this is just
67 hd_src
= fopen(file
, "rb");
69 /* In windows, this will init the winsock stuff */
70 curl_global_init(CURL_GLOBAL_ALL
);
72 /* get a curl handle */
73 curl
= curl_easy_init();
75 /* we want to use our own read function */
76 curl_easy_setopt(curl
, CURLOPT_READFUNCTION
, read_callback
);
78 /* enable uploading */
79 curl_easy_setopt(curl
, CURLOPT_UPLOAD
, 1L);
82 curl_easy_setopt(curl
, CURLOPT_PUT
, 1L);
84 /* specify target URL, and note that this URL should include a file
85 name, not only a directory */
86 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
88 /* now specify which file to upload */
89 curl_easy_setopt(curl
, CURLOPT_READDATA
, hd_src
);
91 /* provide the size of the upload, we specicially typecast the value
92 to curl_off_t since we must be sure to use the correct data size */
93 curl_easy_setopt(curl
, CURLOPT_INFILESIZE_LARGE
,
94 (curl_off_t
)file_info
.st_size
);
96 /* Now run off and do what you've been told! */
97 res
= curl_easy_perform(curl
);
100 curl_easy_cleanup(curl
);
102 fclose(hd_src
); /* close the local file */
104 curl_global_cleanup();