Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libcurl — Keep Connection "open" to Upload Multiple Files (FTP)

Tags:

c

curl

ftp

libcurl

I need to upload directories to a FTP server on my application, and plan to use libcurl. I see there is no direct way to upload a directory with many files, which makes sense to me. I couldn't, however, find any mention on uploading many files.

If I get the list of files in the directory, I could upload them in a loop. The option CURLOPT_FTP_CREATE_MISSING_DIRS might help with sub-directories, but if I'd like to know also if I'm missing the point here or this would have any serious drawback.

The main question is: how can I keep the connection "open"? Reconnecting on each file would probably mean an extra unwanted overhead.

Ideally, I'd like to keep using the easy interface. But if another interface provides better support in this case, I'll use it.

CURLcode ret;
CURL *handle = curl_easy_init();

/* Connect to FTP server using     *
 * the given username and password */

for ({each file}) {

    curl_easy_setopt(handle, ..., ...);
    ...
    ret = curl_easy_perform(handle);
    /* Analyse return code */
    curl_easy_reset(handle);
}

/* Disconnect from server */
curl_easy_clenup(handle);
like image 918
sidyll Avatar asked Jun 20 '11 13:06

sidyll


1 Answers

Just re-use the same handle, and it will keep the connection open as much as possible and subsequent transfers will re-use the previous one.

When you use the easy interface, the connection cache is kept within the easy handle. If you instead use the multi interface, the connection cache will be kept within the multi handle and will be shared among all the easy handles that are used within the same multi handle.

like image 183
Daniel Stenberg Avatar answered Oct 09 '22 02:10

Daniel Stenberg