3

I am new to the libcurl and found a way to download a single file from the ftp server. Now my requirement is to download all files in a directory and i guess it was not supported by libcurl. Kindly suggest on libcurl how to download all files in directory or is there any other library similar to libcurl?

Thanks in advance.

Thi
  • 2,297
  • 7
  • 26
  • 36

3 Answers3

10

Here is a sample piece of code.

static size_t GetFilesList_response(void *ptr, size_t size, size_t nmemb, void *data)
{
    FILE *writehere = (FILE *)data;
    return fwrite(ptr, size, nmemb, writehere);
}

bool FTPWithcURL::GetFilesList(char* tempFile)
{
    CURL *curl;
    CURLcode res;
    FILE *ftpfile;

    /* local file name to store the file as */
    ftpfile = fopen(tempFile, "wb"); /* b is binary, needed on win32 */ 

    curl = curl_easy_init();
    if(curl) 
    {
        curl_easy_setopt(curl, CURLOPT_URL, "ftp://ftp.example.com");
        curl_easy_setopt(curl, CURLOPT_USERPWD, "username:password");
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, ftpfile);
        // added to @Tombart suggestion
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, GetFilesList_response);
        curl_easy_setopt(curl, CURLOPT_DIRLISTONLY, 1);

        res = curl_easy_perform(curl);

        curl_easy_cleanup(curl);
    }

    fclose(ftpfile); //


    if(CURLE_OK != res) 
        return false;

    return true;
}
mtb
  • 1,350
  • 16
  • 32
mentat
  • 2,748
  • 1
  • 21
  • 40
  • 5
    isn't there write function missing? `curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, GetFilesList_response);` – Tombart May 11 '12 at 07:26
0

You need the list of files on the FTP server. Which isn't straightforward as each FTP server might return a different format of file listing...

Anyway, the ftpgetresp.c example shows a way to do it, I think. FTP Custom CUSTOMREQUEST suggests another way.

PhiLho
  • 40,535
  • 6
  • 96
  • 134
  • Hi, Thanks a lot. I was able to retrieve files in a directory using the FTP Custome CUSTOMREQUEST. I got another question, with the multiple files is it recommended to use curl_multi or transferring one file at time? – Thi Jan 29 '10 at 13:45
  • I found another way to retrieve just the files in a directory using CURLOPT_DIRLISTONLY option. I tried using the CUSTOMREQUEST command i need to do lot of parsing but with the CURLOPT_DIRLISTONLY option we can just get the filenames not other information. – Thi Feb 04 '10 at 15:09
0

Just use CURLOPT_WILDCARDMATCH feature. Sample code: https://curl.haxx.se/libcurl/c/ftp-wildcard.html

sdyy
  • 81
  • 3