Class

This class implements the File Transfer Protocol. If you have used a command-line FTP program, and are familiar with the commands, you will be able to use this class easily. Some extra features are included to take advantage of Ruby’s style and strengths.

Example

require 'net/ftp'

Example 1

ftp = Net::FTP.new('example.com')
ftp.login
files = ftp.chdir('pub/lang/ruby/contrib')
files = ftp.list('n*')
ftp.getbinaryfile('nif.rb-0.91.gz', 'nif.gz', 1024)
ftp.close

Example 2

Net::FTP.open('example.com') do |ftp|
  ftp.login
  files = ftp.chdir('pub/lang/ruby/contrib')
  files = ftp.list('n*')
  ftp.getbinaryfile('nif.rb-0.91.gz', 'nif.gz', 1024)
end

Major Methods

The following are the methods most likely to be useful to users:

Constants
No documentation available
No documentation available
No documentation available
No documentation available
No documentation available
No documentation available
Attributes
Read

When true, transfers are performed in binary mode. Default: true.

Read & Write

When true, the connection is in passive mode. Default: true.

Read & Write

When true, use the IP address in PASV responses. Otherwise, it uses the same IP address for the control connection. Default: false.

Read & Write

When true, all traffic to and from the server is written to +$stdout+. Default: false.

Read & Write

Sets or retrieves the resume status, which decides whether incomplete transfers are resumed or restarted. Default: false.

Read & Write

Number of seconds to wait for the connection to open. Any number may be used, including Floats for fractional seconds. If the FTP object cannot open a connection in this many seconds, it raises a Net::OpenTimeout exception. The default value is nil.

Read & Write

Number of seconds to wait for the TLS handshake. Any number may be used, including Floats for fractional seconds. If the FTP object cannot complete the TLS handshake in this many seconds, it raises a Net::OpenTimeout exception. The default value is nil. If ssl_handshake_timeout is nil, open_timeout is used instead.

Number of seconds to wait for one block to be read (via one read(2) call). Any number may be used, including Floats for fractional seconds. If the FTP object cannot read data in this many seconds, it raises a Timeout::Error exception. The default value is 60 seconds.

Read

The server’s welcome message.

The server’s last response code.

Read

The server’s last response code.

The server’s last response.

Class Methods

When true, connections are in passive mode per default. Default: true.

When true, connections are in passive mode per default. Default: true.

Creates and returns a new FTP object. If a host is given, a connection is made.

options is an option hash, each key of which is a symbol.

The available options are:

port

Port number (default value is 21)

ssl

If options is true, then an attempt will be made to use SSL (now TLS) to connect to the server. For this to work OpenSSL [OSSL] and the Ruby OpenSSL [RSSL] extensions need to be installed. If options is a hash, it’s passed to OpenSSL::SSL::SSLContext#set_params as parameters.

private_data_connection

If true, TLS is used for data connections. Default: true when options is true.

username

Username for login. If options is the string “anonymous” and the options is nil, “anonymous@” is used as a password.

password

Password for login.

account

Account information for ACCT.

passive

When true, the connection is in passive mode. Default: true.

open_timeout

Number of seconds to wait for the connection to open. See Net::FTP#open_timeout for details. Default: nil.

read_timeout

Number of seconds to wait for one block to be read. See Net::FTP#read_timeout for details. Default: 60.

ssl_handshake_timeout

Number of seconds to wait for the TLS handshake. See Net::FTP#ssl_handshake_timeout for details. Default: nil.

use_pasv_ip

When true, use the IP address in PASV responses. Otherwise, it uses the same IP address for the control connection. Default: false.

debug_mode

When true, all traffic to and from the server is written to +$stdout+. Default: false.

A synonym for FTP.new, but with a mandatory host parameter.

If a block is given, it is passed the FTP object, which will be closed when the block finishes, or when an exception is raised.

Instance Methods

Aborts the previous command (ABOR command).

Sends the ACCT command.

This is a less common FTP command, to send account information if the destination host requires it.

A setter to toggle transfers in binary mode. newmode is either true or false

Changes the (remote) directory.

Closes the connection. Further operations are impossible until you open a new connection with connect.

Returns true iff the connection is closed.

Establishes an FTP connection to host, optionally overriding the default port. If the environment variable SOCKS_SERVER is set, sets up the connection through a SOCKS proxy. Raises an exception (typically Errno::ECONNREFUSED) if the connection cannot be established.

Deletes a file on the server.

An alias for list

Issues a FEAT command

Returns an array of supported optional features

Retrieves remotefile in whatever mode the session is set (text or binary). See gettextfile and getbinaryfile.

Retrieves remotefile in binary mode, storing the result in localfile. If localfile is nil, returns retrieved data. If a block is supplied, it is passed the retrieved data in blocksize chunks.

An alias for pwd

Retrieves remotefile in ASCII (text) mode, storing the result in localfile. If localfile is nil, returns retrieved data. If a block is supplied, it is passed the retrieved data one line at a time.

Issues the HELP command.

Returns an array of file information in the directory (the output is like ‘ls -l`). If a block is given, it iterates through the listing.

Logs in to the remote host. The session must have been previously connected. If user is the string “anonymous” and the password is nil, “anonymous@” is used as a password. If the acct parameter is not nil, an FTP ACCT command is sent following the successful login. Raises an exception on error (typically Net::FTPPermError).

An alias for list

Returns the raw last modification time of the (remote) file in the format “YYYYMMDDhhmmss” (MDTM command).

Use mtime if you want a parsed Time instance.

Creates a remote directory.

Returns an array of the entries of the directory specified by pathname. Each entry has the facts (e.g., size, last modification time, etc.) and the pathname. If a block is given, it iterates through the listing. If pathname is omitted, the current directory is assumed.

Returns data (e.g., size, last modification time, entry type, etc.) about the file or directory specified by pathname. If pathname is omitted, the current directory is assumed.

Returns the last modification time of the (remote) file. If local is true, it is returned as a local time, otherwise it’s a UTC time.

Returns an array of filenames in the remote directory.

Issues a NOOP command.

Does nothing except return a response.

Issues an OPTS command

  • name Should be the name of the option to set

  • params is any optional parameters to supply with the option

example: option(‘UTF8’, ‘ON’) => ‘OPTS UTF8 ON’

No documentation available
No documentation available
No documentation available
No documentation available

Transfers localfile to the server in whatever mode the session is set (text or binary). See puttextfile and putbinaryfile.

Transfers localfile to the server in binary mode, storing the result in remotefile. If a block is supplied, calls it, passing in the transmitted data in blocksize chunks.

Transfers localfile to the server in ASCII (text) mode, storing the result in remotefile. If callback or an associated block is supplied, calls it, passing in the transmitted data one line at a time.

Returns the current remote directory.

Exits the FTP session.

Setter for the read_timeout attribute.

Renames a file on the server.

Puts the connection into binary (image) mode, issues the given command, and fetches the data returned, passing it to the associated block in chunks of blocksize characters. Note that cmd is a server command (such as “RETR myfile”).

Puts the connection into ASCII (text) mode, issues the given command, and passes the resulting data, one line at a time, to the associated block. If no block is given, prints the lines. Note that cmd is a server command (such as “RETR myfile”).

Removes a remote directory.

Sends a command and returns the response.

Set the socket used to connect to the FTP server.

May raise FTPReplyError if get_greeting is false.

Issues a SITE command.

Returns the size of the given (remote) filename.

No documentation available

Returns the status (STAT command).

pathname

when stat is invoked with pathname as a parameter it acts like list but a lot faster and over the same tcp session.

Puts the connection into binary (image) mode, issues the given server-side command (such as “STOR myfile”), and sends the contents of the file named file to the server. If the optional block is given, it also passes it the data, in chunks of blocksize characters.

Puts the connection into ASCII (text) mode, issues the given server-side command (such as “STOR myfile”), and sends the contents of the file named file to the server, one line at a time. If the optional block is given, it also passes it the lines.

Returns system information.

Sends a command and expect a response beginning with ‘2’.