2
0

ftpuploadfrommem.c 3.8 KB

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