curl-library
Downloading files from an FTP server
Date: Tue, 07 Apr 2009 16:04:18 -0400
I'm trying to just download a bunch of files from an ftp server with a
txt extension, and I'm having a devil of a time figuring out how. I
think part of my problem is that I need to get a listing of the
directory, and only then can I download the files. It seems to me that
libcurl wants to know everything it is doing before it begins the xfer.
Is that true? Am I going to need to make one connection for listing the
files and then another for downloading them? That would be a waste.
The code I have works for the listing, but then when I want to
download the file I get "425 Unable to build data connection: Invalid
argument".
CURL *curl;
CURLcode res;
struct curl_slist *headerlist=NULL;
static const char list [] = "LIST *.txt";
curl_global_init(CURL_GLOBAL_ALL);
/* get a curl handle */
curl = curl_easy_init();
if(curl) {
verify(curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION,
header_callback) == CURLE_OK);
verify(curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
write_callback) == CURLE_OK);
verify(curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, list)
== CURLE_OK);
verify(curl_easy_setopt(curl, CURLOPT_USERPWD,
"xxxxxx:xxxxxx") == CURLE_OK);
/* specify target */
curl_easy_setopt(curl,CURLOPT_URL, "ftp://ftp4.xxxxxx.com/");
/* Now run off and do what you've been told! */
res = curl_easy_perform(curl);
g_pLog->LogLine("Res was: " + LtoA(res));
verify(curl_easy_setopt(curl, CURLOPT_WRITEDATA,
write_data_callback) == CURLE_OK);
headerlist = curl_slist_append(headerlist, "RETR " +
file);//this will be a file that was taken out of the listing during the
previous curl_easy_perform()
/* pass in that last of FTP commands to run */
curl_easy_setopt(curl, CURLOPT_QUOTE, headerlist);
res = curl_easy_perform(curl);
g_pLog->LogLine("Res was: " + LtoA(res));
/* clean up the FTP commands list */
curl_slist_free_all (headerlist);
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
Received on 2009-04-07