cURL / Mailing Lists / curl-library / Single Mail

curl-library

Big problem: uploaded sFtp file's size is 0KB!

From: changqin_gang <changqin_gang_at_sina.com>
Date: Sat, 01 Nov 2008 18:08:58 +0800

As the code below shows, it succesfully created a file on specified sFtp server dierectory, however, the file's size was ZERO! No matter what the local file's size was! &nbsp;

Why? Any thing wrong?

Any help is&nbsp;grateful!

===============CODE==================

static size_t read_callback(void * pBuffer, size_t size, size_t nmemb, void * hFile)
{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DWORD dwNumberOfBytesRead = 0;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BOOL bResult = ReadFile( (HANDLE) hFile, pBuffer, size * nmemb, &dwNumberOfBytesRead, NULL);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return dwNumberOfBytesRead;
}

bool sFTPUpload(CString bsFileFullPath, CString bsFtpWorkPath, CString bsFileRltPath,&nbsp;CString bsUserPassword)
{
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;USES_CONVERSION;

&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;FILE *hFile;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;char * lpszCurlErrorBuffer[CURL_ERROR_SIZE];
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;CURLcode nCurlResult = CURL_LAST;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;struct stat file_info;

&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;DWORD dwLastError;

&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;/*open the uploading file*/
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;int hd ;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;hd = open(bsFileFullPath, O_RDONLY) ;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;fstat(hd, &file_info);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;close(hd) ;

&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;hFile = fopen(bsFileFullPath, "r");
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if (hFile == INVALID_HANDLE_VALUE)
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; dwLastError = GetLastError();
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; printf("open file failed!\n%s\nDiscribe:%s",bsFileFullPath, dwLastError);&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; curl_global_cleanup();
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; return false;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;else&nbsp; printf("open file OK!\n%s\n",bsFileFullPath);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*begin init*/
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; curl_global_init(CURL_GLOBAL_ALL);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CURL * hCurl = curl_easy_init();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!hCurl)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; curl_global_cleanup();
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;printf("CURL-Library easy_init failed \n");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("CURL-Library init OK!\n");

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*begin easy_handle*/
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct curl_slist * headerlist = NULL;
&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;CString sFTPPath = bsFtpWorkPath + bsFileRltPath;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; printf("setopt remote_url:%s\n",sFTPPath);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* enable error buffer */
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; curl_easy_setopt(hCurl, CURLOPT_ERRORBUFFER, lpszCurlErrorBuffer) ;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; curl_easy_setopt(hCurl, CURLOPT_VERBOSE, 1);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* enable uploading */
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; curl_easy_setopt(hCurl, CURLOPT_UPLOAD, TRUE) ;

&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;curl_easy_setopt(hCurl,CURLOPT_TRANSFERTEXT, TRUE) ;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* specify target */&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; curl_easy_setopt(hCurl, CURLOPT_URL, sFTPPath);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* read function */
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; curl_easy_setopt(hCurl, CURLOPT_READFUNCTION, read_callback);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* now specify which file to upload */
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; curl_easy_setopt(hCurl, CURLOPT_READDATA, hFile);

&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;curl_easy_setopt(hCurl, CURLOPT_USERPWD, bsUserPassword);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; curl_easy_setopt(hCurl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Now do perform! */
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nCurlResult = curl_easy_perform(hCurl);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* clean up the FTP commands list */
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; curl_slist_free_all (headerlist);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* always cleanup */
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; curl_easy_cleanup(hCurl);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fclose(hFile); /* close the local file */

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; curl_global_cleanup();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (nCurlResult == CURLE_OK)
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("upload succeed!\n");
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return true;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;
}

int main()
{
&nbsp;&nbsp;&nbsp; CString LocalFilefullpath,ServerWorkPath,sFtpFileRltpath,UserPwd;
&nbsp;&nbsp; &nbsp;LocalFilefullpath = "C:\\Documents and Settings\\user\\desktop\\synctest\\test.txt";
&nbsp;&nbsp;&nbsp; ServerWorkPath = "sftp://192.168.1.166/home/user/";
&nbsp;&nbsp; &nbsp;sFtpFileRltpath = "synctest/test.txt";
&nbsp;&nbsp; &nbsp;UserPwd = "user:pwd";

&nbsp;&nbsp; &nbsp;sFTPUpload(LocalFilefullpath, ServerWorkPath,sFtpFileRltpath,UserPwd);

&nbsp;&nbsp; &nbsp;return 0;
}

-------------------------------------------------------------------
新浪空间——与朋友开心分享网络新生活!(http://space.sina.com.cn/ )
Received on 2008-11-01