12

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);
sidyll
  • 57,726
  • 14
  • 108
  • 151

1 Answers1

14

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.

Daniel Stenberg
  • 54,736
  • 17
  • 146
  • 222
  • Thanks a lot Mr. Stenberg, how proud to receive a response from you! If I may ask to further clarification, will `curl_easy_reset()` be needed in this case? The man page says live connections are kept in the handle, however would re-setting the options manually (in the loop) be enough? – sidyll Jun 20 '11 at 16:54
  • You can do whichever way you think fits your code best. The reset will not kill the connection cache. You can re-set the options manually if you prefer that. – Daniel Stenberg Jun 20 '11 at 20:10
  • Thanks once again, and congratulations for the awesome work. Best wishes – sidyll Jun 20 '11 at 21:28
  • @DanielStenberg : I was looking all over just for this answer. I was worried with multi interface if closing an easy handle would terminate the connection and create new ones for each easy handle which shouldn't be the case. – smRaj Jan 08 '15 at 14:55
  • @DanielStenberg : cURL documentation has had everything I was looking for. `When an easy interface is added to a multi handle, it will use a shared connection cache owned by the multi handle. Removing and adding new easy handles will not affect the pool of connections or the ability to do connection re-use.` Found this in curl_multi_add_handle() :-) – smRaj Jan 08 '15 at 15:36
  • I know - I wrote it! =) – Daniel Stenberg Jan 08 '15 at 15:39
  • I am using libcurl to download a file in chunked blocks from FTP. However, using tcpdump I notice that every time I read a block, there is a login-read-logout. For some reason the connection does not remain persistent. Even though the libcurl is wrapped in a class and the curl_easy_cleanup is performed in the destructor, I don't use curl_easy_reset at all, and I use curl_easy_init in the constructor – radato Oct 21 '18 at 13:54