PureBasic
PureBasic
Create an FTP Upload Plan
See more FTP Examples
Demonstrates the Chilkat Ftp2.CreatePlan method, which creates and returns a textual upload plan for a local directory. The only argument is the local directory. The object must be connected, because the plan records the current absolute remote directory together with the local source paths.
Note: The local paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.
Background: A plan is a saved, resumable description of an upload — the list of local files and their absolute remote destinations — computed once and executed later (or on another run) with
PutPlan. Separating planning from execution suits large or unreliable transfers: the plan can be reviewed, stored, and re-run, and because it records absolute remote paths it stays correct even if the working directory later changes.Chilkat PureBasic Downloads
IncludeFile "CkFtp2.pb"
IncludeFile "CkStringBuilder.pb"
Procedure ChilkatExample()
success.i = 0
; Demonstrates the Ftp2.CreatePlan method, which creates and returns a textual upload plan for a
; local directory. The only argument is the local directory. The object must be connected,
; because the plan records the current absolute remote directory.
ftp.i = CkFtp2::ckCreate()
If ftp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkFtp2::setCkHostname(ftp, "ftp.example.com")
CkFtp2::setCkUsername(ftp, "myFtpLogin")
; Normally you would not hard-code the password in source. You should instead obtain it
; from an interactive prompt, environment variable, or a secrets vault.
CkFtp2::setCkPassword(ftp, "myPassword")
success = CkFtp2::ckConnect(ftp)
If success = 0
Debug CkFtp2::ckLastErrorText(ftp)
CkFtp2::ckDispose(ftp)
ProcedureReturn
EndIf
success = CkFtp2::ckChangeRemoteDir(ftp,"public_html")
If success = 0
Debug CkFtp2::ckLastErrorText(ftp)
CkFtp2::ckDispose(ftp)
ProcedureReturn
EndIf
; The plan captures which local files to upload and the absolute remote destination. Save it so
; the upload can be run (or resumed) later with PutPlan.
plan.s = CkFtp2::ckCreatePlan(ftp,"qa_data/site")
If CkFtp2::ckLastMethodSuccess(ftp) = 0
Debug CkFtp2::ckLastErrorText(ftp)
CkFtp2::ckDispose(ftp)
ProcedureReturn
EndIf
; Persist the plan text for later use.
sbPlan.i = CkStringBuilder::ckCreate()
If sbPlan.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckAppend(sbPlan,plan)
success = CkStringBuilder::ckWriteFile(sbPlan,"qa_output/upload_plan.txt")
If success = 0
Debug CkStringBuilder::ckLastErrorText(sbPlan)
CkFtp2::ckDispose(ftp)
CkStringBuilder::ckDispose(sbPlan)
ProcedureReturn
EndIf
Debug "Upload plan created."
success = CkFtp2::ckDisconnect(ftp)
If success = 0
Debug CkFtp2::ckLastErrorText(ftp)
CkFtp2::ckDispose(ftp)
CkStringBuilder::ckDispose(sbPlan)
ProcedureReturn
EndIf
CkFtp2::ckDispose(ftp)
CkStringBuilder::ckDispose(sbPlan)
ProcedureReturn
EndProcedure