Lua
Lua
Change the Remote Directory (CWD)
See more FTP Examples
Demonstrates the Chilkat Ftp2.ChangeRemoteDir method, which changes the current working directory on the FTP server. The only argument is the remote directory path.
Background: This sends a single
CWD with the path exactly as given; Chilkat does not split a multi-level path into one CWD per segment, so whether a/b/c works in one call depends on the server. A relative path moves from the current directory, while an absolute path (leading /) is resolved from the root according to the server's conventions. Confirm the result with GetCurrentRemoteDir when it matters.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- Demonstrates the Ftp2.ChangeRemoteDir method, which changes the current working directory on
-- the FTP server. The only argument is the remote directory path.
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"
-- Connect and log in (Connect performs both).
local success = ftp:connect()
if not success then
print(ftp.last_error_text)
return
end
-- A relative path is resolved from the current remote directory; an absolute path (beginning
-- with "/") is interpreted according to the server's path syntax.
success = ftp:change_remote_dir("public_html/images")
if not success then
print(ftp.last_error_text)
return
end
local currentDir = ftp:get_current_remote_dir()
if not ftp.last_method_success then
print(ftp.last_error_text)
return
end
print("Now in: " .. currentDir)
success = ftp:disconnect()
if not success then
print(ftp.last_error_text)
return
end