Sample code for 30+ languages & platforms
Lua

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 Lua Downloads

Lua
local chilkat = require("chilkat")

-- 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.

local ftp = chilkat.Ftp2.new()

ftp.hostname = "ftp.example.com"
ftp.username = "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.
ftp.password = "myPassword"

local success = ftp:connect()
if not success then
    print(ftp.last_error_text)
    return
end

success = ftp:change_remote_dir("public_html")
if not success then
    print(ftp.last_error_text)
    return
end

-- 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.
local plan = ftp:create_plan("qa_data/site")
if not ftp.last_method_success then
    print(ftp.last_error_text)
    return
end

-- Persist the plan text for later use.
local sbPlan = chilkat.StringBuilder.new()
sbPlan:append(plan)
success = sbPlan:write_file("qa_output/upload_plan.txt", "utf-8", false)
if not success then
    print(sbPlan.last_error_text)
    return
end

print("Upload plan created.")

success = ftp:disconnect()
if not success then
    print(ftp.last_error_text)
    return
end