fileupload.c 1.7 KB

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