Sample code for 30+ languages & platforms
PureBasic

Upgrade an FTP Control Channel to TLS (ConvertToTls)

See more FTP Examples

Demonstrates the Chilkat Ftp2.ConvertToTls method, which upgrades an already-connected clear FTP control channel to explicit TLS. It takes no arguments and is used with a manually staged connection created by ConnectOnly while Ssl is disabled.

Background: This exposes the explicit-FTPS AUTH TLS upgrade as a discrete step for the rare case where you need to do something on the clear connection first — inspect the greeting, apply a workaround — before securing it. In the normal case you simply set AuthTls and let Connect perform the upgrade automatically; this manual path exists for staged control.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkFtp2.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the Ftp2.ConvertToTls method, which upgrades an already-connected clear FTP control
    ;  channel to explicit TLS.  It takes no arguments.  It is used with a manually staged connection
    ;  created by ConnectOnly while Ssl is disabled.

    ftp.i = CkFtp2::ckCreate()
    If ftp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkFtp2::setCkHostname(ftp, "ftp.example.com")
    CkFtp2::setCkPort(ftp, 21)

    ;  Connect in clear text first (Ssl is false).
    success = CkFtp2::ckConnectOnly(ftp)
    If success = 0
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        ProcedureReturn
    EndIf

    ;  Upgrade the control channel to explicit TLS (AUTH TLS).
    success = CkFtp2::ckConvertToTls(ftp)
    If success = 0
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        ProcedureReturn
    EndIf

    ;  Now authenticate over the encrypted control channel.
    CkFtp2::setCkUsername(ftp, "myFtpLogin")
    CkFtp2::setCkPassword(ftp, "myPassword")
    success = CkFtp2::ckLoginAfterConnectOnly(ftp)
    If success = 0
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        ProcedureReturn
    EndIf

    Debug "Control channel upgraded to TLS and logged in."

    success = CkFtp2::ckDisconnect(ftp)
    If success = 0
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        ProcedureReturn
    EndIf



    CkFtp2::ckDispose(ftp)


    ProcedureReturn
EndProcedure