ftpupload.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * $Id: ftpupload.c,v 1.4 2004/01/05 22:29:30 bagder Exp $
  9. */
  10. #include <stdio.h>
  11. #include <curl/curl.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <fcntl.h>
  15. /*
  16. * This example shows an FTP upload, with a rename of the file just after
  17. * a successful upload.
  18. *
  19. * Example based on source code provided by Erick Nuwendam. Thanks!
  20. */
  21. #define LOCAL_FILE "/tmp/uploadthis.txt"
  22. #define UPLOAD_FILE_AS "while-uploading.txt"
  23. #define REMOTE_URL "ftp://localhost/" UPLOAD_FILE_AS
  24. #define RENAME_FILE_TO "renamed-and-fine.txt"
  25. int main(int argc, char **argv)
  26. {
  27. CURL *curl;
  28. CURLcode res;
  29. FILE *ftpfile;
  30. FILE * hd_src ;
  31. int hd ;
  32. struct stat file_info;
  33. struct curl_slist *headerlist=NULL;
  34. char buf_1 [] = "RNFR " UPLOAD_FILE_AS;
  35. char buf_2 [] = "RNTO " RENAME_FILE_TO;
  36. /* get the file size of the local file */
  37. hd = open(LOCAL_FILE, O_RDONLY) ;
  38. fstat(hd, &file_info);
  39. close(hd) ;
  40. /* get a FILE * of the same file, could also be made with
  41. fdopen() from the previous descriptor, but hey this is just
  42. an example! */
  43. hd_src = fopen(LOCAL_FILE, "rb");
  44. /* In windows, this will init the winsock stuff */
  45. curl_global_init(CURL_GLOBAL_ALL);
  46. /* get a curl handle */
  47. curl = curl_easy_init();
  48. if(curl) {
  49. /* build a list of commands to pass to libcurl */
  50. headerlist = curl_slist_append(headerlist, buf_1);
  51. headerlist = curl_slist_append(headerlist, buf_2);
  52. /* enable uploading */
  53. curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ;
  54. /* specify target */
  55. curl_easy_setopt(curl,CURLOPT_URL, REMOTE_URL);
  56. /* pass in that last of FTP commands to run after the transfer */
  57. curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
  58. /* now specify which file to upload */
  59. curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
  60. /* and give the size of the upload (optional) */
  61. curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, file_info.st_size);
  62. /* Now run off and do what you've been told! */
  63. res = curl_easy_perform(curl);
  64. /* clean up the FTP commands list */
  65. curl_slist_free_all (headerlist);
  66. /* always cleanup */
  67. curl_easy_cleanup(curl);
  68. }
  69. fclose(hd_src); /* close the local file */
  70. curl_global_cleanup();
  71. return 0;
  72. }