ftpuploadfrommem.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  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 https://curl.haxx.se/docs/copyright.html.
  13. *
  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.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. /* <DESC>
  23. * FTP upload a file from memory
  24. * </DESC>
  25. */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <curl/curl.h>
  29. static const char data[]=
  30. "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
  31. "Nam rhoncus odio id venenatis volutpat. Vestibulum dapibus "
  32. "bibendum ullamcorper. Maecenas finibus elit augue, vel "
  33. "condimentum odio maximus nec. In hac habitasse platea dictumst. "
  34. "Vestibulum vel dolor et turpis rutrum finibus ac at nulla. "
  35. "Vivamus nec neque ac elit blandit pretium vitae maximus ipsum. "
  36. "Quisque sodales magna vel erat auctor, sed pellentesque nisi "
  37. "rhoncus. Donec vehicula maximus pretium. Aliquam eu tincidunt "
  38. "lorem.";
  39. struct WriteThis {
  40. const char *readptr;
  41. size_t sizeleft;
  42. };
  43. static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
  44. {
  45. struct WriteThis *upload = (struct WriteThis *)userp;
  46. size_t max = size*nmemb;
  47. if(max < 1)
  48. return 0;
  49. if(upload->sizeleft) {
  50. size_t copylen = max;
  51. if(copylen > upload->sizeleft)
  52. copylen = upload->sizeleft;
  53. memcpy(ptr, upload->readptr, copylen);
  54. upload->readptr += copylen;
  55. upload->sizeleft -= copylen;
  56. return copylen;
  57. }
  58. return 0; /* no more data left to deliver */
  59. }
  60. int main(void)
  61. {
  62. CURL *curl;
  63. CURLcode res;
  64. struct WriteThis upload;
  65. upload.readptr = data;
  66. upload.sizeleft = strlen(data);
  67. /* In windows, this will init the winsock stuff */
  68. res = curl_global_init(CURL_GLOBAL_DEFAULT);
  69. /* Check for errors */
  70. if(res != CURLE_OK) {
  71. fprintf(stderr, "curl_global_init() failed: %s\n",
  72. curl_easy_strerror(res));
  73. return 1;
  74. }
  75. /* get a curl handle */
  76. curl = curl_easy_init();
  77. if(curl) {
  78. /* First set the URL, the target file */
  79. curl_easy_setopt(curl, CURLOPT_URL,
  80. "ftp://example.com/path/to/upload/file");
  81. /* User and password for the FTP login */
  82. curl_easy_setopt(curl, CURLOPT_USERPWD, "login:secret");
  83. /* Now specify we want to UPLOAD data */
  84. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  85. /* we want to use our own read function */
  86. curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  87. /* pointer to pass to our read function */
  88. curl_easy_setopt(curl, CURLOPT_READDATA, &upload);
  89. /* get verbose debug output please */
  90. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  91. /* Set the expected upload size. */
  92. curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
  93. (curl_off_t)upload.sizeleft);
  94. /* Perform the request, res will get the return code */
  95. res = curl_easy_perform(curl);
  96. /* Check for errors */
  97. if(res != CURLE_OK)
  98. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  99. curl_easy_strerror(res));
  100. /* always cleanup */
  101. curl_easy_cleanup(curl);
  102. }
  103. curl_global_cleanup();
  104. return 0;
  105. }