Cannot upload file with libcurl

Hello,
I'm trying to upload a file to a FTP server with curl. The problem is that I do not know what to do after I enter the passive mode and open the specified port. I tried to send data with curl_easy_send but it simply hangs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
bool Ftp::Upload(const char *local_fn,const char *remote_fn)
{
    int ret;
    string ip;
    unsigned port;

    SendCmd("TYPE I");
// Enter passive mode and get IP & port
    if(pasv(ip,port)==false)
        return false;

   cout <<"IP: "<<ip<<" port: "<<port<<endl;

// STOR filename
    if(remote_fn==0)
        SendCmd(string("STOR ")+PathParser(local_fn).GetFileName(to_string(time(0))),ftp_read_file,fp);
    else
        SendCmd(string("STOR ")+remote_fn,ftp_read_file,fp);

//
// open a seocnd curl connection for file upload
//
    CURL *curl_up=curl_easy_init();
    curl_easy_setopt(curl_up, CURLOPT_URL,ip.c_str()); // ip and port received with PASV
    curl_easy_setopt(curl_up, CURLOPT_PORT,port);
    // connect
    if( (ret=curl_easy_perform(curl))!=CURLE_OK)
    {
        // ignore CURLE_FTP_COULDNT_RETR_FILE (19) This was either a weird reply to a 'RETR' command or a zero byte transfer complete.
        // maybe i shouldnt ignroe it
        if(ret!=19)
        {
            cout <<"Cannot establish connection. code "<<ret<<endl;
            return false;
        }
    }
    cout <<"Begin file transfer..."<<endl;

// What should I do here?
    char buff[]="this should be written to the file on FTP server";
    size_t rec; // byhtes received

    if( (ret=curl_easy_send(curl_up,buff,sizeof(buff),&rec))!=CURLE_OK) // doesn't crash but never finishes
    {
        fclose(fp);
        curl_easy_cleanup(curl_up);
        cout <<"curl_easy_send failed: "<<ret<<endl;
        return false;
    }
    fclose(fp);
    curl_easy_cleanup(curl_up);
    return true;
}

I have no idea how to send file to the server. And I also suspect I should tell the file size to the server.

Thanks for help!
Maybe you need curl_easy_setopt( curl, CURLOPT_USERPWD, "username:password" );
Didn't help. The last reply from server is
150 Opening data channel for file upload to server of "/test.rar"

I just need to upload the data somehow
Any reason why don't you use libcurl own example ?
http://curl.haxx.se/libcurl/c/ftpupload.html

Passive mode is the default, it can be disabled with :
curl_easy_setopt(curlhandle, CURLOPT_FTPPORT, "-");
Last edited on
Any reason why don't you use libcurl own example ?
No particular reason, just learning how things work. I'll just use libcurl example, thanks.
Topic archived. No new replies allowed.