Programmatically uploading a file to an FTP site

Raymond Chen

Today’s Little Program uploads a file to an FTP site in binary mode with the assistance of the Wininet library. This program has sat in my bag of tools for years.

#define STRICT
#define UNICODE
#include <windows.h>
#include <wininet.h>
#include <shellapi.h>
int __cdecl wmain(int argc, PWSTR argv[])
{
 if (argc == 6) {
  HINTERNET hintRoot = InternetOpen(TEXT("ftpput/1.0"),
            INTERNET_OPEN_TYPE_DIRECT,
            NULL, NULL, 0);
  if (hintRoot) {
   HINTERNET hintFtp = InternetConnect(hintRoot,
            argv[1],
            INTERNET_DEFAULT_FTP_PORT,
            argv[2],
            argv[3],
            INTERNET_SERVICE_FTP,
            INTERNET_FLAG_PASSIVE,
            NULL);
   if (hintFtp) {
    FtpPutFile(hintFtp, argv[4], argv[5],
         FTP_TRANSFER_TYPE_BINARY,
         NULL);
    InternetCloseHandle(hintFtp);
   }
   InternetCloseHandle(hintRoot);
  }
 }
 return 0;
}

The program accepts five command line arguments:

  1. site (no “ftp://” in front)
  2. userid
  3. password
  4. path for the file to upload
  5. location to place the uploaded file

For example, I might say ftpput ftp.contoso.com admin seinfeld newversion.zip subdir/newversion.zip

0 comments

Discussion is closed.

Feedback usabilla icon