i'm trying ftp file via curl , receiving 405 method not allowed. i've followed example the documentation, no luck far. additions example curl_easy_setopt( curl, curlopt_userpwd, "*******:*******" ) , ftp address. i've spent of time figuring out correct ftp url format , current error result of believe right.
note: when using #define remote_url "ftp://*********.com/home/*********/public_html/data/", curl returns url using bad/illegal format or missing url
#include <stdio.h> #include <string.h> #include <curl/curl.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <errno.h> #ifdef win32 #include <io.h> #else #include <unistd.h> #endif /* <desc> * performs ftp upload , renames file after successful * transfer. * </desc> */ #define local_file "data.csv" #define upload_file_as "ftpd.csv" #define remote_url "http://*********.com/home/*********/public_html/data/" #define rename_file_to "renamed-and-fine.txt" /* note: if want example work on windows libcurl dll, must provide read callback curlopt_readfunction. failing give crash since dll may not use variable's memory when passed in app this. */ static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream) { curl_off_t nread; /* in real-world cases, data differently fread() stuff library default internally */ size_t retcode = fread(ptr, size, nmemb, (file*) stream); nread = (curl_off_t)retcode; fprintf(stderr, "*** read %" curl_format_curl_off_t " bytes file\n", nread); return retcode; } int main(void) { curl *curl; curlcode res; file *hd_src; struct stat file_info; curl_off_t fsize; struct curl_slist *headerlist=null; static const char buf_1 [] = "rnfr " upload_file_as; static const char buf_2 [] = "rnto " rename_file_to; /* file size of local file */ if(stat(local_file, &file_info)) { printf("couldnt open '%s': %s\n", local_file, strerror(errno)); return 1; } fsize = (curl_off_t)file_info.st_size; printf("local file size: %" curl_format_curl_off_t " bytes.\n", fsize); /* file * of same file */ hd_src = fopen(local_file, "rb"); /* in windows, init winsock stuff */ curl_global_init(curl_global_all); /* curl handle */ curl = curl_easy_init(); if(curl) { /* build list of commands pass libcurl */ headerlist = curl_slist_append(headerlist, buf_1); headerlist = curl_slist_append(headerlist, buf_2); /* want use our own read function */ curl_easy_setopt(curl, curlopt_readfunction, read_callback); /* enable uploading */ curl_easy_setopt(curl, curlopt_upload, 1l); /* specify target */ curl_easy_setopt(curl,curlopt_url, remote_url); /* username , password */ curl_easy_setopt( curl, curlopt_userpwd, "*******:*******" ); /* pass in last of ftp commands run after transfer */ curl_easy_setopt(curl, curlopt_postquote, headerlist); /* specify file upload */ curl_easy_setopt(curl, curlopt_readdata, hd_src); /* set size of file upload (optional). if give *_large option must make sure type of passed-in argument curl_off_t. if use curlopt_infilesize (without _large) must make sure pass in type 'long' argument. */ curl_easy_setopt(curl, curlopt_infilesize_large, (curl_off_t)fsize); /* run off , you've been told! */ res = curl_easy_perform(curl); /* check errors */ if(res != curle_ok) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); /* clean ftp commands list */ curl_slist_free_all (headerlist); /* cleanup */ curl_easy_cleanup(curl); } fclose(hd_src); /* close local file */ curl_global_cleanup(); return 0; }
your remote_url starts http://. thus, curl connects http server @ 80 port , tries send non-http commands. ftp different service @ different port (21) , requires ftp:// schema curl connect.
Comments
Post a Comment