ftpupload.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * $Id$
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <curl/curl.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <fcntl.h>
  16. #include <errno.h>
  17. #ifdef WIN32
  18. #include <io.h>
  19. #else
  20. #include <unistd.h>
  21. #endif
  22. /*
  23. * This example shows an FTP upload, with a rename of the file just after
  24. * a successful upload.
  25. *
  26. * Example based on source code provided by Erick Nuwendam. Thanks!
  27. */
  28. #define LOCAL_FILE "/tmp/uploadthis.txt"
  29. #define UPLOAD_FILE_AS "while-uploading.txt"
  30. #define REMOTE_URL "ftp://localhost/" UPLOAD_FILE_AS
  31. #define RENAME_FILE_TO "renamed-and-fine.txt"
  32. /* NOTE: if you want this example to work on Windows with libcurl as a
  33. DLL, you MUST also provide a read callback with CURLOPT_READFUNCTION.
  34. Failing to do so will give you a crash since a DLL may not use the
  35. variable's memory when passed in to it from an app like this. */
  36. static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
  37. {
  38. /* in real-world cases, this would probably get this data differently
  39. as this fread() stuff is exactly what the library already would do
  40. by default internally */
  41. size_t retcode = fread(ptr, size, nmemb, stream);
  42. fprintf(stderr, "*** We read %d bytes from file\n", retcode);
  43. return retcode;
  44. }
  45. int main(int argc, char **argv)
  46. {
  47. CURL *curl;
  48. CURLcode res;
  49. FILE *hd_src;
  50. struct stat file_info;
  51. struct curl_slist *headerlist=NULL;
  52. static const char buf_1 [] = "RNFR " UPLOAD_FILE_AS;
  53. static const char buf_2 [] = "RNTO " RENAME_FILE_TO;
  54. /* get the file size of the local file */
  55. if(stat(LOCAL_FILE, &file_info)) {
  56. printf("Couldnt open '%s': %s\n", LOCAL_FILE, strerror(errno));
  57. return 1;
  58. }
  59. printf("Local file size: %ld bytes.\n", file_info.st_size);
  60. /* get a FILE * of the same file */
  61. hd_src = fopen(LOCAL_FILE, "rb");
  62. /* In windows, this will init the winsock stuff */
  63. curl_global_init(CURL_GLOBAL_ALL);
  64. /* get a curl handle */
  65. curl = curl_easy_init();
  66. if(curl) {
  67. /* build a list of commands to pass to libcurl */
  68. headerlist = curl_slist_append(headerlist, buf_1);
  69. headerlist = curl_slist_append(headerlist, buf_2);
  70. /* we want to use our own read function */
  71. curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  72. /* enable uploading */
  73. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  74. /* specify target */
  75. curl_easy_setopt(curl,CURLOPT_URL, REMOTE_URL);
  76. /* pass in that last of FTP commands to run after the transfer */
  77. curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
  78. /* now specify which file to upload */
  79. curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
  80. /* Set the size of the file to upload (optional). If you give a *_LARGE
  81. option you MUST make sure that the type of the passed-in argument is a
  82. curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
  83. make sure that to pass in a type 'long' argument. */
  84. curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
  85. (curl_off_t)file_info.st_size);
  86. /* Now run off and do what you've been told! */
  87. res = curl_easy_perform(curl);
  88. /* clean up the FTP commands list */
  89. curl_slist_free_all (headerlist);
  90. /* always cleanup */
  91. curl_easy_cleanup(curl);
  92. }
  93. fclose(hd_src); /* close the local file */
  94. curl_global_cleanup();
  95. return 0;
  96. }