Erlang -- ftp

User's Guide
Reference Manual
Release Notes
PDF
Top

inets
Reference Manual
Version 6.0

Expand All
Contract All

Table of Contents

- inets
  - Top of manual page
  - services/0
  - services_info/0
  - service_names/0
  - start/0
  - start/1
  - stop/0
  - start/2
  - start/3
  - stop/2
- ftp
  - Top of manual page
  - account/2
  - append/2
  - append/3
  - append_bin/3
  - append_chunk/2
  - append_chunk_start/2
  - append_chunk_end/1
  - cd/2
  - close/1
  - delete/2
  - formaterror/1
  - lcd/2
  - lpwd/1
  - ls/1
  - ls/2
  - mkdir/2
  - nlist/1
  - nlist/2
  - open/1
  - open/2
  - pwd/1
  - pwd/1
  - recv/2
  - recv/3
  - recv_bin/2
  - recv_chunk_start/2
  - recv_chunk/1
  - rename/3
  - rmdir/2
  - send/2
  - send/3
  - send_bin/3
  - send_chunk/2
  - send_chunk_start/2
  - send_chunk_end/1
  - type/2
  - user/3
  - user/4
  - quote/2
- tftp
  - Top of manual page
  - start/1
  - read_file/3
  - write_file/3
  - info/1
  - info/1
  - info/1
  - change_config/2
  - change_config/2
  - change_config/2
  - start/0
  - prepare/6
  - open/6
  - read/1
  - write/2
  - abort/3
  - error_msg/2
  - warning_msg/2
  - info_msg/2
- httpc
  - Top of manual page
  - request/1
  - request/2
  - request/4
  - request/5
  - cancel_request/1
  - cancel_request/2
  - set_options/1
  - set_options/2
  - get_options/1
  - get_options/2
  - stream_next/1
  - store_cookies/2
  - store_cookies/3
  - cookie_header/1
  - cookie_header/2
  - cookie_header/3
  - reset_cookies/0
  - reset_cookies/1
  - which_cookies/0
  - which_cookies/1
  - which_sessions/0
  - which_sessions/1
  - info/0
  - info/1
- httpd
  - Top of manual page
  - info/1
  - info/2
  - info/2
  - info/3
  - info/4
  - info/3
  - reload_config/2
  - Module:do/1
  - Module:load/2
  - Module:store/2
  - Module:remove/1
  - parse_query/1
- httpd_conf
  - Top of manual page
  - check_enum/2
  - clean/1
  - custom_clean/3
  - is_directory/1
  - is_file/1
  - make_integer/1
- httpd_custom_api
  - Top of manual page
  - response_header/1
  - request_header/1
- httpd_socket
  - Top of manual page
  - deliver/3
  - peername/2
  - resolve/0
- httpd_util
  - Top of manual page
  - convert_request_date/1
  - create_etag/1
  - decode_hex/1
  - day/1
  - flatlength/1
  - hexlist_to_integer/1
  - integer_to_hexlist/1
  - lookup/2
  - lookup/3
  - lookup_mime/2
  - lookup_mime/3
  - lookup_mime_default/2
  - lookup_mime_default/3
  - message/3
  - month/1
  - multi_lookup/2
  - reason_phrase/1
  - rfc1123_date/0
  - rfc1123_date/2
  - split/3
  - split_script_path/1
  - split_path/1
  - strip/1
  - suffix/1
- mod_alias
  - Top of manual page
  - default_index/2
  - path/3
  - real_name/3
  - real_script_name/3
- mod_auth
  - Top of manual page
  - add_user/2
  - add_user/5
  - add_user/6
  - delete_user/2
  - delete_user/3
  - delete_user/4
  - get_user/2
  - get_user/3
  - get_user/4
  - list_users/1
  - list_users/2
  - list_users/3
  - add_group_member/3
  - add_group_member/4
  - add_group_member/5
  - delete_group_member/3
  - delete_group_member/4
  - delete_group_member/5
  - list_group_members/2
  - list_group_members/3
  - list_group_members/4
  - list_groups/1
  - list_groups/2
  - list_groups/3
  - delete_group/2
  - delete_group/4
  - update_password/5
  - update_password/6
- mod_esi
  - Top of manual page
  - deliver/2
  - Module:Function/3
  - Module:Function/2
- mod_security
  - Top of manual page
  - list_auth_users/1
  - list_auth_users/2
  - list_auth_users/2
  - list_auth_users/3
  - list_blocked_users/1
  - list_blocked_users/2
  - list_blocked_users/2
  - list_blocked_users/3
  - block_user/4
  - block_user/5
  - unblock_user/2
  - unblock_user/3
  - unblock_user/3
  - unblock_user/4
  - event/4
  - event/5
- http_uri
  - Top of manual page
  - scheme_defaults/0
  - parse/1
  - parse/2
  - encode/1
  - decode/1

# ftp

### MODULE

ftp

### MODULE SUMMARY

A File Transfer Protocol client

### DESCRIPTION

The ftp module implements a client for file transfer according to a subset of the File Transfer Protocol (see RFC959).

Starting from inets version 4.4.1 the ftp client will always try to use passive ftp mode and only resort to active ftp mode if this fails. There is a start option mode where this default behavior may be changed.

There are two ways to start an ftp client. One is using the Inets service framework and the other is to start it directy as a standalone process using the open function.

For a simple example of an ftp session see Inets User's Guide.

In addition to the ordinary functions for receiving and sending files (see recv/2, recv/3, send/2 and send/3) there are functions for receiving remote files as binaries (see recv_bin/2) and for sending binaries to to be stored as remote files (see send_bin/3).

There is also a set of functions for sending and receiving contiguous parts of a file to be stored in a remote file (for send see send_chunk_start/2, send_chunk/2 and send_chunk_end/1 and for receive see recv_chunk_start/2 and recv_chunk/).

The particular return values of the functions below depend very much on the implementation of the FTP server at the remote host. In particular the results from ls and nlist varies. Often real errors are not reported as errors by ls, even if for instance a file or directory does not exist. nlist is usually more strict, but some implementations have the peculiar behaviour of responding with an error, if the request is a listing of the contents of directory which exists but is empty.

### FTP CLIENT SERVICE START/STOP

The FTP client can be started and stopped dynamically in runtime by calling the Inets application API inets:start(ftpc, ServiceConfig), or inets:start(ftpc, ServiceConfig, How), and inets:stop(ftpc, Pid). See inets(3) for more info.

Below follows a description of the available configuration options.

{host, Host}

  Host = string() | ip_address()

{port, Port}

  Port = integer() > 0

  Default is 21.

{mode, Mode}

  Mode = active | passive

  Default is passive.

{verbose, Verbose}

  Verbose = boolean()

  This determines if the FTP communication should be verbose or not.

  Default is false.

{debug, Debug}

  Debug = trace | debug | disable

  Debugging using the dbg toolkit.

  Default is disable.

{ipfamily, IpFamily}

  IpFamily = inet | inet6 | inet6fb4

  With inet6fb4 the client behaves as before (it tries to use IPv6 and only if that does not work, it uses IPv4).

  Default is inet (IPv4).

{timeout, Timeout}

  Timeout = non_neg_integer()

  Connection timeout.

  Default is 60000 (milliseconds).

{dtimeout, DTimeout}

  DTimeout = non_neg_integer() | infinity

  Data Connect timeout. The time the client will wait for the server to connect to the data socket.

  Default is infinity.

{progress, Progress}

  Progress = ignore | {CBModule, CBFunction, InitProgress}

  CBModule = atom(), CBFunction = atom()

  InitProgress = term()

  Default is ignore.

The progress option is intended to be used by applications that want to create some type of progress report such as a progress bar in a GUI. The default value for the progress option is ignore e.i. the option is not used. When the progress option is specified the following will happen when ftp:send/[3,4] or ftp:recv/[3,4] are called.

- Before a file is transfered the following call will be made to indicate the start of the file transfer and how big the file is. The return value of the callback function should be a new value for the UserProgressTerm that will bu used as input next time the callback function is called.

  CBModule:CBFunction(InitProgress, File, {file_size, FileSize})

- Every time a chunk of bytes is transfered the following call will be made:

  CBModule:CBFunction(UserProgressTerm, File, {transfer_size, TransferSize})

- At the end of the file the following call will be made to indicate the end of the transfer.

  CBModule:CBFunction(UserProgressTerm, File, {transfer_size, 0})

The callback function should be defined as

CBModule:CBFunction(UserProgressTerm, File, Size) -> UserProgressTerm

CBModule = CBFunction = atom()

UserProgressTerm = term()

File = string()

Size = {transfer_size, integer()} | {file_size, integer()} | {file_size, unknown}

Alas for remote files it is not possible for ftp to determine the file size in a platform independent way. In this case the size will be unknown and it is left to the application to find out the size.

Note

The callback is made by a middleman process, hence the file transfer will not be affected by the code in the progress callback function. If the callback should crash this will be detected by the ftp connection process that will print an info-report and then go one as if the progress option was set to ignore.

The file transfer type is set to the default of the FTP server when the session is opened. This is usually ASCCI-mode.

The current local working directory (cf. lpwd/1) is set to the value reported by file:get_cwd/1. the wanted local directory.

The return value Pid is used as a reference to the newly created ftp client in all other functions, and they should be called by the process that created the connection. The ftp client process monitors the process that created it and will terminate if that process terminates.

### COMMON DATA TYPES

Here follows type definitions that are used by more than one function in the FTP client API.

pid() - identifier of an ftp connection.

string() = list of ASCII characters.

shortage_reason() = etnospc | epnospc

restriction_reason() = epath | efnamena | elogin | enotbinary - note not all restrictions may always relevant to all functions

common_reason() = econn | eclosed | term() - some kind of explanation of what went wrong.

### EXPORTS

account(Pid, Account) -> ok | {error, Reason}

Types:

Pid = pid()
Account = string()
Reason = eacct | common_reason()

If an account is needed for an operation set the account with this operation.

append(Pid, LocalFile) ->
append(Pid, LocalFile, RemoteFile) -> ok | {error, Reason}

Types:

Pid = pid()
LocalFile = RemoteFile = string()
Reason = epath | elogin | etnospc | epnospc | efnamena | common_reason

Transfers the file LocalFile to the remote server. If RemoteFile is specified, the name of the remote file that the file will be appended to is set to RemoteFile; otherwise the name is set to LocalFile If the file does not exists the file will be created.

append_bin(Pid, Bin, RemoteFile) -> ok | {error, Reason}

Types:

Pid = pid()
Bin = binary()()
RemoteFile = string()
Reason = restriction_reason()| shortage_reason() | common_reason()

Transfers the binary Bin to the remote server and append it to the file RemoteFile. If the file does not exists it will be created.

append_chunk(Pid, Bin) -> ok | {error, Reason}

Types:

Pid = pid()
Bin = binary()
Reason = echunk | restriction_reason() | common_reason()

Transfer the chunk Bin to the remote server, which append it into the file specified in the call to append_chunk_start/2.

Note that for some errors, e.g. file system full, it is necessary to to call append_chunk_end to get the proper reason.

append_chunk_start(Pid, File) -> ok | {error, Reason}

Types:

Pid = pid()
File = string()
Reason = restriction_reason() | common_reason()

Start the transfer of chunks for appending to the file File at the remote server. If the file does not exists it will be created.

append_chunk_end(Pid) -> ok | {error, Reason}

Types:

Pid = pid()
Reason = echunk | restriction_reason() | shortage_reason()

Stops transfer of chunks for appending to the remote server. The file at the remote server, specified in the call to append_chunk_start/2 is closed by the server.

cd(Pid, Dir) -> ok | {error, Reason}

Types:

Pid = pid()
Dir = string()
Reason = restriction_reason() | common_reason()

Changes the working directory at the remote server to Dir.

close(Pid) -> ok

Types:

Pid = pid()

Ends an ftp session, created using the open function.

delete(Pid, File) -> ok | {error, Reason}

Types:

Pid = pid()
File = string()
Reason = restriction_reason() | common_reason()

Deletes the file File at the remote server.

formaterror(Tag) -> string()

Types:

Tag = {error, atom()} | atom()

Given an error return value {error, AtomReason}, this function returns a readable string describing the error.

lcd(Pid, Dir) -> ok | {error, Reason}

Types:

Pid = pid()
Dir = string()
Reason = restriction_reason()

Changes the working directory to Dir for the local client.

lpwd(Pid) -> {ok, Dir}

Types:

Pid = pid()

Returns the current working directory at the local client.

ls(Pid) ->
ls(Pid, Pathname) -> {ok, Listing} | {error, Reason}

Types:

Pid = pid()
Pathname = string()
Listing = string()
Reason = restriction_reason() | common_reason()

Returns a list of files in long format.

Pathname can be a directory, a group of files or even a file. The Pathname string can contain wildcard(s).

ls/1 implies the user's current remote directory.

The format of Listing is operating system dependent (on UNIX it is typically produced from the output of the ls -l shell command).

mkdir(Pid, Dir) -> ok | {error, Reason}

Types:

Pid = pid()
Dir = string()
Reason = restriction_reason() | common_reason()

Creates the directory Dir at the remote server.

nlist(Pid) ->
nlist(Pid, Pathname) -> {ok, Listing} | {error, Reason}

Types:

Pid = pid()
Pathname = string()
Listing = string()
Reason = restriction_reason() | common_reason()

Returns a list of files in short format.

Pathname can be a directory, a group of files or even a file. The Pathname string can contain wildcard(s).

nlist/1 implies the user's current remote directory.

The format of Listing is a stream of file names, where each name is separated by <CRLF> or <NL>. Contrary to the ls function, the purpose of nlist is to make it possible for a program to automatically process file name information.

open(Host) -> {ok, Pid} | {error, Reason}
open(Host, Opts) -> {ok, Pid} | {error, Reason}

Types:

Host = string() | ip_address()
Opts = options()
options() = [option()]
option() = start_option() | open_option()
start_option() = {verbose, verbose()} | {debug, debug()}
verbose() = boolean() (defaults to false)
debug() = disable | debug | trace (defaults to disable)
open_option() = {ipfamily, ipfamily()} | {port, port()} | {mode, mode()} | {tls, tls_options()} | {timeout, timeout()} | {dtimeout, dtimeout()} | {progress, progress()}
ipfamily() = inet | inet6 | inet6fb4 (defaults to inet)
port() = integer() > 0 (defaults to 21)
mode() = active | passive (defaults to passive)
tls_options() = [ssl:ssloption()]
timeout() = integer() > 0 (defaults to 60000 milliseconds)
dtimeout() = integer() > 0 | infinity (defaults to infinity)
pogress() = ignore | {module(), function(), initial_data()} (defaults to ignore)
module() = atom()
function() = atom()
initial_data() = term()
Reason = ehost | term()

This function is used to start a standalone ftp client process (without the inets service framework) and open a session with the FTP server at Host.

If the option {tls, tls_options()} is present, the ftp session will be transported over tls (ftps, see RFC 4217). The list tls_options() may be empty. The function ssl:connect/3 is used for securing both the control connection and the data sessions.

A session opened in this way, is closed using the close function.

pwd(Pid) -> {ok, Dir} | {error, Reason}

Types:

Pid = pid()
Reason = restriction_reason() | common_reason()

Returns the current working directory at the remote server.

pwd(Pid) -> {ok, Dir} | {error, Reason}

Types:

Pid = pid()
Reason = restriction_reason() | common_reason()

Returns the current working directory at the remote server.

recv(Pid, RemoteFile) ->
recv(Pid, RemoteFile, LocalFile) -> ok | {error, Reason}

Types:

Pid = pid()
RemoteFile = LocalFile = string()
Reason = restriction_reason() | common_reason() | file_write_error_reason()
file_write_error_reason() = see file:write/2

Transfer the file RemoteFile from the remote server to the the file system of the local client. If LocalFile is specified, the local file will be LocalFile; otherwise it will be RemoteFile.

If the file write fails (e.g. enospc), then the command is aborted and {error, file_write_error_reason()} is returned. The file is however not removed.

recv_bin(Pid, RemoteFile) -> {ok, Bin} | {error, Reason}

Types:

Pid = pid()
Bin = binary()
RemoteFile = string()
Reason = restriction_reason() | common_reason()

Transfers the file RemoteFile from the remote server and receives it as a binary.

recv_chunk_start(Pid, RemoteFile) -> ok | {error, Reason}

Types:

Pid = pid()
RemoteFile = string()
Reason = restriction_reason() | common_reason()

Start transfer of the file RemoteFile from the remote server.

recv_chunk(Pid) -> ok | {ok, Bin} | {error, Reason}

Types:

Pid = pid()
Bin = binary()
Reason = restriction_reason() | common_reason()

Receive a chunk of the remote file (RemoteFile of recv_chunk_start). The return values has the following meaning:

- ok the transfer is complete.
- {ok, Bin} just another chunk of the file.
- {error, Reason} transfer failed.

rename(Pid, Old, New) -> ok | {error, Reason}

Types:

Pid = pid()
CurrFile = NewFile = string()
Reason = restriction_reason() | common_reason()

Renames Old to New at the remote server.

rmdir(Pid, Dir) -> ok | {error, Reason}

Types:

Pid = pid()
Dir = string()
Reason = restriction_reason() | common_reason()

Removes directory Dir at the remote server.

send(Pid, LocalFile) ->
send(Pid, LocalFile, RemoteFile) -> ok | {error, Reason}

Types:

Pid = pid()
LocalFile = RemoteFile = string()
Reason = restriction_reason() | common_reason() | shortage_reason()

Transfers the file LocalFile to the remote server. If RemoteFile is specified, the name of the remote file is set to RemoteFile; otherwise the name is set to LocalFile.

send_bin(Pid, Bin, RemoteFile) -> ok | {error, Reason}

Types:

Pid = pid()
Bin = binary()()
RemoteFile = string()
Reason = restriction_reason() | common_reason() | shortage_reason()

Transfers the binary Bin into the file RemoteFile at the remote server.

send_chunk(Pid, Bin) -> ok | {error, Reason}

Types:

Pid = pid()
Bin = binary()
Reason = echunk | restriction_reason() | common_reason()

Transfer the chunk Bin to the remote server, which writes it into the file specified in the call to send_chunk_start/2.

Note that for some errors, e.g. file system full, it is necessary to to call send_chunk_end to get the proper reason.

send_chunk_start(Pid, File) -> ok | {error, Reason}

Types:

Pid = pid()
File = string()
Reason = restriction_reason() | common_reason()

Start transfer of chunks into the file File at the remote server.

send_chunk_end(Pid) -> ok | {error, Reason}

Types:

Pid = pid()
Reason = restriction_reason() | common_reason() | shortage_reason()

Stops transfer of chunks to the remote server. The file at the remote server, specified in the call to send_chunk_start/2 is closed by the server.

type(Pid, Type) -> ok | {error, Reason}

Types:

Pid = pid()
Type = ascii | binary
Reason = etype | restriction_reason() | common_reason()

Sets the file transfer type to ascii or binary. When an ftp session is opened, the default transfer type of the server is used, most often ascii, which is the default according to RFC 959.

user(Pid, User, Password) -> ok | {error, Reason}

Types:

Pid = pid()
User = Password = string()
Reason = euser | common_reason()

Performs login of User with Password.

user(Pid, User, Password, Account) -> ok | {error, Reason}

Types:

Pid = pid()
User = Password = string()
Reason = euser | common_reason()

Performs login of User with Password to the account specified by Account.

quote(Pid, Command) -> [FTPLine]

Types:

Pid = pid()
Command = string()
FTPLine = string() - Note the telnet end of line characters, from the ftp protocol definition, CRLF e.g. "\\r\\n" has been removed.

Sends an arbitrary FTP command and returns verbatimly a list of the lines sent back by the FTP server. This functions is intended to give an application accesses to FTP commands that are server specific or that may not be provided by this FTP client.

Note

FTP commands that require a data connection can not be successfully issued with this function.

### ERRORS

The possible error reasons and the corresponding diagnostic strings returned by formaterror/1 are as follows:

echunk

  Synchronisation error during chunk sending.

  A call has been made to send_chunk/2 or send_chunk_end/1, before a call to send_chunk_start/2; or a call has been made to another transfer function during chunk sending, i.e. before a call to send_chunk_end/1.

eclosed

  The session has been closed.

econn

  Connection to remote server prematurely closed.

ehost

  Host not found, FTP server not found, or connection rejected by FTP server.

elogin

  User not logged in.

enotbinary

  Term is not a binary.

epath

  No such file or directory, or directory already exists, or permission denied.

etype

  No such type.

euser

  User name or password not valid.

etnospc

  Insufficient storage space in system [452].

epnospc

  Exceeded storage allocation (for current directory or dataset) [552].

efnamena

  File name not allowed [553].

### SEE ALSO

file, filename, J. Postel and J. Reynolds: File Transfer Protocol (RFC 959).