LuaSocket
Network support for the Lua language

home · download · introduction · reference


FTP

FTP (File Transfer Protocol) is a protocol used to transfer files between hosts. The module ftp.lua offers simple FTP support, allowing applications to download and upload files, and list directory contents. The implementation conforms to RFC 959.

URLs MUST conform to RFC 1738, that is, an URL is a string in the form:

[ftp://][<user>[:<password>]@]<host>[:<port>][/<path>][type=a|i|d]

socket.ftp.get(url)
socket.ftp.get{
  url = string,
  type = string,
  user = string,
  password = string
}

Downloads an URL from a FTP server.

The function can be called either directly with a url or with a request table. Fields passed explicitly in the request table override those present in the url.

The parameter type accepts values 'a' (ASCII, the default), 'i' (binary) or 'd' (directory listing) and determines the transfer type. If <path> ends with a '/' or type is 'd', a directory listing of <path> is returned. If no user is provided in the url or explicitly, the function tries to log in as user 'anonymous'.

If successful, the function returns the file content as a string. In case of error, the function returns nil and an error message describing the error.

-- Log as user "anonymous" on server "ftp.tecgraf.puc-rio.br",
-- go to directory "pub/lua" and get file "lua.tar.gz" as binary.
f, e = socket.ftp.get("ftp://ftp.tecgraf.puc-rio.br/pub/lua/lua.tar.gz;type=i")

-- Log as user "anonymous" on server "ftp.tecgraf.puc-rio.br",
-- go to director "pub" and retrieve directory listing of directory "lua"
f, e = socket.ftp.get("ftp://ftp.tecgraf.puc-rio.br/pub/lua;type=d")

-- Log as user "diego", password "nehab", on server "ftp.tecgraf.puc-rio.br",
-- go to directory "tec/luasocket/bin" and retrieve file "luasocket.exe"
-- (actually, fails because of wrong password, of course)
f, e = socket.ftp.get{
  url = "ftp://ftp.tecgraf.puc-rio.br/tec/luasocket/bin/luasocket.exe",
  user = "diego",
  password = "nehab",
  type = "i"
}
-- f returns nil, and e returns an appropriate error message

socket.ftp.get_cb{
  url = string,
  type = string,
  content_cb = receive-callback,
  user = string,
  password = string
}

Same as get, but the library returns the content of the downloaded file to the receive callback content_cb.

Note: for more information on callbacks, refer to Streaming with callbacks.

socket.ftp.put(url, content)
socket.ftp.put{
  url = string,
  content = string,
  type = string,
  user = string,
  password = string
}

Upload a file to a FTP server.

The function can be called directly with a url and content parameters, or with a request table. Values passed explicitly in the request table override those present in the url. The parameter type accept values 'a' (ASCII, the default) or 'i' (binary) and determines the transfer type. If no user is provided, the function tries to log in as 'anonymous'.

If successful, the function returns 1. In case of error, the function returns nil followed by a string describing the error.

-- Log as user "anonymous" on server "ftp.free.org" and store file
-- "hello" with contents "hello world!", using binary mode for the transfer
r, e = socket.ftp.put("ftp://ftp.free.org/hello;type=i", "hello world!\n")

-- Does exactly the same, but logging in as diego
r, e = socket.ftp.put{
  url = "ftp://ftp.free.org/hello",
  type = "i",
  user = "diego",
  password = "nehab",
  content = "hello world\n"
}

socket.ftp.put_cb{
  url = string,
  type = string,
  content_cb = send-callback,
  user = string,
  password = string
}

Same as put, but the library obtains the contents of the file to be uploaded using the send callback content_cb.

Note: for more information on callbacks, refer to Streaming with callbacks.

-- Log as user "anonymous" on server "ftp.free.org" and store file
-- "hello" with contents of the same file in the current directory, 
-- using binary mode for the transfer
r, e = socket.ftp.put_cb{
  url = "ftp://ftp.free.org/hello",
  type = "i",
  content_cb = socket.callback.send_file(io.open("hello", "r"))
}