I am programming in C++
and I want to upload a file to my web server which i have already done using the following code:
#include "stdafx.h"
#include <windows.h>
#include <wininet.h>
#pragma comment(lib, "wininet")
int _tmain(int argc, _TCHAR* argv[])
{
HINTERNET hInternet;
HINTERNET hFtp;
hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL,NULL,0);
hFtp = InternetConnect(hInternet, L"ftp.freetzi.com", INTERNET_DEFAULT_FTP_PORT, L"myuser.freetzi.com", "mypassword", INTERNET_SERVICE_FTP, 0, 0);
FtpPutFile(hFtp, L"C:\\deneme.txt", L"coolName.txt", FTP_TRANSFER_TYPE_BINARY, 0);
if ( FtpPutFile(hFtp, L"C:\\deneme.txt", L"coolName.txt", FTP_TRANSFER_TYPE_BINARY, 0) )
{
MessageBox(NULL, L"Upload Successful.", L"Title", NULL);
}
else
{
//This is the message I get...
MessageBox(NULL, L"Upload Failed.", L"Title", NULL);
}
InternetCloseHandle(hFtp);
InternetCloseHandle(hInternet);
return 0;
}
But as this program will be used by many people, I can't find a way to rename the uploaded file coolname.txt
if the target file already exists, the code above will overwrite the old data and rewrite the new one of used 2 or more times,
I tried:
- "/public_html/log.txt" + std::to_string(rand())
2 . A function :-
generateFileName( std::string const& directory,
std::string const& rootName,
std::string const& extension )
{
std::ostringstream results;
results.fill( '0' );
results << directory << '/' << rootName << '-' << std::setw(4) << rand() << extension;
return results.str();
}
I however, found a way to accomplish this task using FtpFindFirstFile
But I am unable to know how to use it. If someone could explain me more clear or give some examples, I would really appriciate.
Reference : http://msdn.microsoft.com/en-us/library/windows/desktop/aa384180(v=vs.85).aspx