Click here to Skip to main content
15,900,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
I have one simple code for FTP files to my server.

C++
#include <windows.h>
#include <wininet.h>
#include <winnt.h>

using namespace std;

int main() {
    //LPCTSTR server = _T("ftp.server.com/public_html/data");
    LPCTSTR server = _T("www.server.com");


INTERNET_PORT port = INTERNET_DEFAULT_FTP_PORT;
LPCTSTR user = _T("user");
LPCTSTR pass = _T("pass");
DWORD service = INTERNET_SERVICE_FTP;
DWORD context = 0;
LPCTSTR szLocalFile = _T("C:\\ss.csv");
LPCTSTR szRemoteFile = _T("ss.csv");


    HINTERNET hSession = InternetOpen(0, INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0);
    HINTERNET hService = InternetConnect(hSession, server, INTERNET_DEFAULT_FTP_PORT, user, pass, INTERNET_SERVICE_FTP, 0, 0);
    FtpPutFile(hService, szLocalFile, szLocalFile, FTP_TRANSFER_TYPE_BINARY, 0);
    if((FtpPutFile(hService, szLocalFile, szRemoteFile, FTP_TRANSFER_TYPE_BINARY, 0))==false) cout<<"false";
    InternetCloseHandle(hService);
    InternetCloseHandle(hSession);
    cout<<"Finished";
   
    return 0;
}


When I am using server="www.server.com" it works correctly but my ftp address is include path too.
When I try do server="ftp.server.com/public_html/data" not works and no transfer any file.
How to add my FTP path to this code for transfer code?
Regards,
Posted
Updated 27-Aug-11 5:00am
v2

1 solution

With InternetConnect[^] you are only supposed to specify the server name you want to connect to. After making the connection use FtpSetCurrentDirectory[^] to change the folder on the FTP server.

By the way please note that you are uploading the file two times.
Replace:
C++
FtpPutFile(hService, szLocalFile, szLocalFile, FTP_TRANSFER_TYPE_BINARY, 0);
if((FtpPutFile(hService, szLocalFile, szRemoteFile, FTP_TRANSFER_TYPE_BINARY, 0))==false) cout<<"false";

with:
C++
if( !FtpPutFile(hService, szLocalFile, szRemoteFile, FTP_TRANSFER_TYPE_BINARY, 0) )
	cout<<"false";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900