cURL / Mailing Lists / curl-library / Single Mail


Downloading files from an FTP server

From: Angus March <>
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

        CURL *curl;
        CURLcode res;
        struct curl_slist *headerlist=NULL;
        static const char list [] = "LIST *.txt";

        /* 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)
            verify(curl_easy_setopt(curl, CURLOPT_USERPWD,
"xxxxxx:xxxxxx") == CURLE_OK);

            /* specify target */
            curl_easy_setopt(curl,CURLOPT_URL, "");

            /* 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 */

Received on 2009-04-07