1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 * HTTP PUT with easy interface and read callback
29 #include <curl/curl.h>
32 * This example shows a HTTP PUT operation. PUTs a file given as a command
33 * line argument to the URL also given on the command line.
35 * This example also uses its own read callback.
37 * Here's an article on how to setup a PUT handler for Apache:
38 * http://www.apacheweek.com/features/put
41 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
46 /* in real-world cases, this would probably get this data differently
47 as this fread() stuff is exactly what the library already would do
48 by default internally */
49 retcode = fread(ptr, size, nmemb, stream);
51 nread = (curl_off_t)retcode;
53 fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
54 " bytes from file\n", nread);
59 int main(int argc, char **argv)
64 struct stat file_info;
75 /* get the file size of the local file */
76 stat(file, &file_info);
78 /* get a FILE * of the same file, could also be made with
79 fdopen() from the previous descriptor, but hey this is just
81 hd_src = fopen(file, "rb");
83 /* In windows, this will init the winsock stuff */
84 curl_global_init(CURL_GLOBAL_ALL);
86 /* get a curl handle */
87 curl = curl_easy_init();
89 /* we want to use our own read function */
90 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
92 /* enable uploading */
93 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
96 curl_easy_setopt(curl, CURLOPT_PUT, 1L);
98 /* specify target URL, and note that this URL should include a file
99 name, not only a directory */
100 curl_easy_setopt(curl, CURLOPT_URL, url);
102 /* now specify which file to upload */
103 curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
105 /* provide the size of the upload, we specicially typecast the value
106 to curl_off_t since we must be sure to use the correct data size */
107 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
108 (curl_off_t)file_info.st_size);
110 /* Now run off and do what you've been told! */
111 res = curl_easy_perform(curl);
112 /* Check for errors */
114 fprintf(stderr, "curl_easy_perform() failed: %s\n",
115 curl_easy_strerror(res));
118 curl_easy_cleanup(curl);
120 fclose(hd_src); /* close the local file */
122 curl_global_cleanup();