Class wwSFTP

This class provides an SFTP client that works with SSH based secure FTP connections. This is the most common secure FTP format used for *nix based servers and most widely supported.

Does not support FTPS

SFTP and FTPS are two completely separate protocols and this library only supports SSH based SFTP, not FTPS which is FTP over TSL.

This class is based on wwFtp and supports the same interface for the most part - there are subtle differences in how directory listings are handled (you should only pass in paths, not wildcards which are invalid for the SSH client). Like wwFtp this class supports both high level and lower level routines for sending files.

High Level Upload and Download

The High level functions are easy functions to upload and download files FtpGetFile() and FtpSendFile() which contain all the parameters needed to send and receive a single file.

loFtp = CREATEOBJECT("wwSftp")
loFtp.nFtpPort = 23

lcHost = "127.0.0.1"
lnPort = 23
lcUsername = "tester"
lcPassword = "password"

*** Download
lcOutputFile = ".\tests\sailbig.jpg"
DELETE FILE lcOutputFile

lnResult = loFtp.FtpGetFile(lcHost,"sailbig.jpg",".\tests\sailbig.jpg",1,lcUsername,lcPassword)

this.AssertTrue(lnResult == 0,loFtp.cErrorMsg)
this.AssertTrue(FILE(lcOutputFile))


*** Upload a file
lcSourceFile = ".\tests\sailbig.jpg"
lcTargetFile = "Sailbig2.jpg"

lnResult = loFtp.FtpSendFile(lcHost,lcSourceFile,lcTargetFile,lcUsername,lcPassword)
this.AssertTrue(lnResult == 0,loFtp.cErrorMsg)

Low Level Functions

The lower level functions require that you set properties on the wwSFTP object, make a connection and then use the individual methods to Send and receive files, change directories, delete files, create folders etc. The advantage of this mechanism is that you have more control over the process as well as keeping a connection open between requests.

The following is an integration test that demonstrates a host of the lower level features:

loFtp = CREATEOBJECT("wwSftp")

loFtp.cFtpServer =  "127.0.0.1"
loFtp.nFtpPort = 23
loFtp.cUsername = "tester"
loFtp.cPassword = "password"

loFtp.FtpConnect()

*** Change to a specific folder - convenience only - you can reference relative paths
this.AssertTrue(loFtp.FtpSetDirectory("subfolder"),loFtp.cErrorMsg)

*** Create a new directory
this.AssertTrue(loFtp.FtpCreateDirectory("SubFolder2"),loFtp.cErrorMsg)

*** Send a file into the new directory
this.AssertTrue(loFtp.FtpSendFileEx(".\tests\sailbig.jpg","subfolder2/sailbig.jpg")==0,loFtp.cErrorMsg)

*** Download the file just uploaded
this.AssertTrue(loFtp.FtpGetFileEx("subfolder2/sailbig.jpg",".\tests\sailbig2.jpg")==0,loFtp.cErrorMsg)

*** Delete it
this.AssertTrue(loFtp.FtpDeleteFile("subfolder2/sailbig.jpg")==0,loFtp.cErrorMsg)

*** And delete the folder
this.AssertTrue(loFtp.FtpRemoveDirectory("Subfolder2"),loFtp.cErrorMsg)

Class Members

MemberDescription

FtpClose

Closes the FTP Connection.

o.FtpClose()

FtpConnect

o.FtpConnect(lcServer, lcUsername, lcPassword)

FtpCreateDirectory

Creates a directory on the server.

o.FtpCreateDirectory(lcPath)

FtpDeleteFile

Deletes a file on the FTP server.

o.FtpDeleteFile(lcFile)

FtpGetFile

High level function to download a file from the SFTP server.

o.FtpGetFile(lcFTPServer, lcFtpSource, lcLocalTarget, lnBinary, lcUsername, lcPassword)

FtpGetFileEx

Downloads a file from the Ftp server to a local folder. Make sure to call FtpConnect() before calling this method.

o.FtpGetFileEx(lcFtpSourceFile, lcLocalTargetFile)

FtpListFiles

o.FtpListFiles(lcPath)

FtpRemoveDirectory

Removes a directory from the FTP server.

o.FtpRemoveDirectory(lcPath)

FtpRenameFile

Renames a file on the server without deleting and reuploading.

You can use paths with this function allowing you to essentially move files to a different folder.

o.FtpRenameFile(lcOldname, lcNewName)

FtpSendFile

Single method call to send a file to the FTP server.

o.FtpSendFile(lcFTPServer, lcSource, lcTarget, lcUsername, lcPassword)

FtpSendFileEx

Sends a file to the server on an already open connection. Make sure you have called FtpOpenConnection() before calling this method.

o.FtpSendFileEx(lcLocalSourceFile, lcFtpTargetFile)

FtpSetDirectory

Changes the active server path to the specified folder.

o.FtpSetDirectory(lcNewPath)

Init

Stock Properties

Init

Function: Assume: Pass: Return:

o.Init()

Load

Load

Function: Assume: Pass: Return:

o.Load()

cFtpServer

cPassword

cPrivateKeyFile

cPrivateKeyFilePassword

An optional password for a keyfile if provided in the cPrivateKeyFile property.

For more info see the wwSFTP::cPrivateKeyFile documentation.

cUsername

nFtpPort

Specifies the FTP port to connect to on the server.

oSFTP

Instance of the SftpFtpCLient .NET wrapper class.

oSFTPClient

Instance of the SSH.NET SftpClient object. Accessible here to allow additional configuration options.

Requirements

Assembly: wwsftp.prg

© West Wind Technologies, 1996-2018 • Updated: 03/21/17
Comment or report problem with topic