Git init
[external/curl.git] / docs / examples / fileupload.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  */
9
10 #include <stdio.h>
11 #include <curl/curl.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14
15 int main(void)
16 {
17   CURL *curl;
18   CURLcode res;
19   struct stat file_info;
20   double speed_upload, total_time;
21   FILE *fd;
22
23   fd = fopen("debugit", "rb"); /* open file to upload */
24   if(!fd) {
25
26     return 1; /* can't continue */
27   }
28
29   /* to get the file size */
30   if(fstat(fileno(fd), &file_info) != 0) {
31
32     return 1; /* can't continue */
33   }
34
35   curl = curl_easy_init();
36   if(curl) {
37     /* upload to this place */
38     curl_easy_setopt(curl, CURLOPT_URL,
39                      "file:///home/dast/src/curl/debug/new");
40
41     /* tell it to "upload" to the URL */
42     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
43
44     /* set where to read from (on Windows you need to use READFUNCTION too) */
45     curl_easy_setopt(curl, CURLOPT_READDATA, fd);
46
47     /* and give the size of the upload (optional) */
48     curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
49                      (curl_off_t)file_info.st_size);
50
51     /* enable verbose for easier tracing */
52     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
53
54     res = curl_easy_perform(curl);
55
56     /* now extract transfer info */
57     curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
58     curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);
59
60     fprintf(stderr, "Speed: %.3f bytes/sec during %.3f seconds\n",
61             speed_upload, total_time);
62
63     /* always cleanup */
64     curl_easy_cleanup(curl);
65   }
66   return 0;
67 }