libcurl上传本地文件/图片到ftp
int main()
{
char url = "ftp://……";
char fileName = "test.txt";
FILE* fp = fopen(fileName,"rb");
if(fp == NULL)
{
cout<<"file open failed!"<<endl;
return -1;
}
fseek(fp,0L,SEEK_END);
long fileSize = ftell(fp);
fseek(fp,0L,SEEK_SET);
curl_global_init(CURL_GLOBAL_ALL);
CURL* curl = curl_easy_init();
if(!curl)
{
fclose(fp);
cout<<"curl init failed!"<<endl;
return -1;
}
curl_easy_setopt(curl,CURLOPT_READDATE,fp);
curl_easy_setopt(curl,CURLOPT_INFILESIZE,fileSize);
curl_easy_setopt(curl,CURLOPT_UPLOAD,1L);
curl_easy_setopt(curl,CURLOPT_URL,url);
curl_easy_setopt(curl,CURLOPT_FTP_CREATE_MISSING_DIRS,1L);
CURLcode result = curl_easy_perform(curl);
if(result != CURLE_OK)
{
cout<<"error type : "<<result<<endl;
curl_easy_cleanup(curl);
curl_global_cleanup();
fclose(fp);
return -1;
}
curl_easy_cleanup(curl);
curl_global_cleanup();
fclose(fp);
return 0;
}