TFtpClient / TFtpSession

Source: source/classes/tftpcli.prg (TFtpClient), source/classes/tftpsess.prg (TFtpSession)

FiveWin provides FTP client capabilities through TFtpClient, which implements the FTP protocol for file transfer operations. It supports uploading (STOR), downloading (RETR), directory navigation (CWD, PWD), directory creation (MKD), file deletion (DELE), and binary/text transfer modes (TYPE). TFtpSession is a lightweight class used by the internal FTP server to track connected client sessions.

TFtpClient Class

DATA Members

DATATypeDescription
cIPServerCharacterFTP server hostname or IP address
nPortNumericFTP server port (default 21)
UserCharacterFTP username
PassCharacterFTP password
oSocketTSocketControl connection socket
oDataSocketTSocketData connection socket for file transfers
nStatusNumericSession state (CONNECTING, IDENTIFY1, IDENTIFY2)
AllOkLogicalSet to .T. after successful login
SrvLastMsgCharacterLast server response message
lWaitingLogicalBusy flag during synchronous command execution
lTimeOutLogicalSet to .T. if a command times out (30 seconds)
nSendedNumericBytes sent counter
bConnectingBlockCallback during connection
bConnectedBlockCallback when connected
bDoneBlockCallback when operation completes

Methods

MethodDescription
New( cIPServer, nPort, User, Pass )Create FTP client and connect to server. nPort defaults to 21.
PWD()Print working directory. Returns server response.
CWD( cDirName )Change working directory on the server
MKD( cDirName )Create a directory on the server
DELE( cFileName )Delete a file from the server
TYPE( cType )Set transfer mode: "I" (binary/image) or "A" (ASCII)
STOR( cFileName, bBlock )Upload a file (STORE). Uses PORT active mode.
RETR( cFileName )Download a file (RETRIEVE). Returns .T. on success, .F. if file not found (550).
PORT( oTransSocket )Generate PORT command string for active data connection
QUIT()Disconnect from the server
DoWait()Wait synchronously for server response (30-second timeout)

TFtpSession Class

TFtpSession is a simple data class used internally to represent a connected FTP client session on the server side:

DATATypeDescription
cUserNameCharacterAuthenticated username
cPasswordCharacterAuthenticated password
cCurDirCharacterCurrent working directory for the session
cIPCharacterClient IP address from PORT command
nPortNumericClient port from PORT command
oSocketTSocketResponse socket for the session

Example: Upload a File

#include "FiveWin.ch"

function Main()

   local oFtp

   oFtp := TFtpClient():New( "ftp.example.com", 21, ;
                             "username", "password" )

   if oFtp:AllOk
      oFtp:CWD( "/public_html" )
      oFtp:STOR( "C:\files\index.html" )
      oFtp:QUIT()
      MsgInfo( "Upload complete" )
   else
      MsgStop( "FTP connection failed" )
   endif

return nil

Example: Download a File

#include "FiveWin.ch"

function Main()

   local oFtp, lResult

   oFtp := TFtpClient():New( "ftp.example.com", 21, ;
                             "username", "password" )

   if oFtp:AllOk
      oFtp:CWD( "/backups" )
      lResult := oFtp:RETR( "data_2026.zip" )
      if lResult
         MsgInfo( "Download successful" )
      else
         MsgAlert( "File not found on server" )
      endif
      oFtp:QUIT()
   endif

return nil

Example: Directory Operations

#include "FiveWin.ch"

function Main()

   local oFtp, cCurrentDir

   oFtp := TFtpClient():New( "ftp.example.com", 21, ;
                             "username", "password" )

   if oFtp:AllOk
      // Get current directory
      cCurrentDir := oFtp:PWD()

      // Create a directory
      oFtp:MKD( "new_folder" )

      // Change into it
      oFtp:CWD( "new_folder" )

      // List current directory
      ? oFtp:PWD()

      oFtp:QUIT()
   endif

return nil

Notes

See Also