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;
}
|