Note: This class is not available with the PO_NO_NETWORK parse option.
The FtpClient class allows Qore programs to communicate with FTP servers. The constructor takes an optional URL with the following format:
[(ftp|ftps)://][username[:password]@]hostname[:port]
If the URL is not set with the constructor, then the connection parameters must be set with the FtpClient::set*() methods. At the very minimum the hostname must be set. If any path name is given in the URL, it is ignored. See the following table for default URL parameters.
Table 4.229. FtpClient::constructor() Default URL Parameters
Field | Default Value |
|---|---|
|
|
|
|
|
|
|
|
Once the URL (at least the hostname) has been set, any method requiring a connection will make an implicit call to FtpClient::connect() to attempt to connect and login to the FTP server before executing the method.
Objects of this class are capable of making encrypted FTPS connections according to RFC-4217. TLS/SSL encrypted control and data connection will be attempted if the protocol is set to 'ftps' or the FtpClient::setSecure() method is called before connecting.
Note that 'sftp', or FTP over ssh, is not supported with this class; FTPS is an extension of the FTP protocol to allow for secure connections; while 'sftp' is FTP over an encrypted ssh connection. For sftp and ssh support, use the ssh2 module.
When a data connection is required, by default the following modes are tried in series: EPSV (Extended Passive Mode), PASV (Passive Mode), and PORT (Port mode). If the FTP server does not support one of these methods, or network conditions do not allow a data connection of any of these types to be established, then an exception is thrown.
To manually control which modes are tried, see the FtpClient::setModeEPSV(), FtpClient::setModePASV(), and FtpClient::setModePORT() methods.
This class supports posting network events to a Queue, either events on the control or data channels or both can be monitored. See Event Handling for more information.
The events raised by this object are listed in the following table:
Table 4.230. FtpClient Events
Name | Value | Description |
|---|---|---|
| Raised immediately before an FTP control message is sent. | |
| Raised when an FTP reply is received on the control channel. |
Table 4.231. FtpClient Method Overview
Method | Except? | Description |
|---|---|---|
|
| Y | Creates object and optionally initializes URL |
N | Destroys the object. | |
Y | Throws an exception to prevent copying of objects this class. | |
Y | Connects and logs in to FTP server | |
N | Disconnects from the FTP server. | |
|
|
Y |
Make an FTPS connection to the server on the next connect if the argument is |
|
Y |
Make a non-encrypted connection to the server on the next connect. | |
|
Y |
Make a non-encrypted data connection to the server on the next connect even if the control connection is secure. | |
|
N |
Returns | |
|
N |
Returns | |
|
N |
Returns the name of the cipher for an encrypted connection or NOTHING if no encrypted connection has been established. | |
|
N |
Returns the version string of the cipher for encrypted connections or NOTHING if no encrypted connection has been established. | |
|
N |
Returns a string code giving the result of verifying the remote certificate or NOTHING if no encrypted connection has been established. | |
|
N |
Sets the object to automatically try to negotiate the data connections in EPSV, PASV, and PORT modes, in this order. | |
|
N |
Sets the object to only try to make data connections using EPSV (RFC-2428 extended passive) mode. | |
|
N |
Sets the object to only try to make data connections using PASV (RFC-959 passive) mode. | |
|
N |
Sets the object to only try to make data connections using PORT mode. | |
|
| Y | Gets a list of files from the FTP server in the server's long format; returns NOTHING if the path cannot be found. |
|
| Y | Gets a list of file names from the FTP server; returns NOTHING if the path cannot be found. |
Y | Returns the server-side current working directory. | |
|
| Y | Changes the current working directory on the server. |
|
| Y | Gets a file from the FTP server and stores it on the local filesystem; if any errors occur, an exception is thrown.. |
|
| Y | Gets a file from the FTP server and returns it as a binary value. |
|
| Y | Gets a file from the FTP server and returns it as a string. |
|
| Y | Transfers a file to the FTP server; if any errors occur, an exception is thrown. |
|
| Y | Transfers a data to the FTP server and saves it as a file on the remote machine; if any errors occur, an exception is thrown. |
|
| Y | Deletes a file from the FTP server |
|
| Y | Creates a directory on the FTP server |
|
| Y | Removes a directory on the FTP server |
|
| Y | Renames/moves a file or directory; if any errors occur, an exception is thrown. |
|
| N | Sets the user name to use |
|
| N | Sets the password to use |
|
| N | Sets the hostname to connect to |
|
| Y | Sets the port to connect to; throws an exception if the port is <= 0. |
|
| Y | Sets the URL |
N | Gets the current user name or NOTHING if not yet set. | |
N | Gets the current password or NOTHING if not yet set. | |
N | Gets the current hostname or NOTHING if not yet set. | |
N | Gets the current port number. | |
N | Gets the current FTP URL. | |
|
| N | Sets a Queue object to receive socket and FtpClient events on both the data and control connections. Calling with no argument clears the queue. |
|
| N | Sets a Queue object to receive socket and FtpClient events on the data connection. Calling with no argument clears the queue. |
N | Sets a Queue object to receive socket and FtpClient events on the control connection. Calling with no argument clears the queue. |
Creates the FtpClient object. It accepts one optional argument that will set the URL for the FTP connection; the path is ignored in the URL, however the username, password, hostname and port are respected; additionally if the protocol is "ftps", the client will attempt to establish a secure connection to the server according to RFC-4217 when the first connection is established.
FtpClient::constructor(string $url)
my FtpClient $ftp("ftp://user:pass@hostname");
Table 4.232. Arguments for FtpClient::constructor()
Argument | Description |
|---|---|
| The URL of the server to connect to; must have at least the hostname to connect to. |
Table 4.233. Exceptions Thrown by FtpClient::constructor()
err | desc |
|---|---|
| Only "ftp" or "ftps" are allowed as the protocol in the URL. |
| No hostname given in the URL. |
Destroys the object.
delete $ftp;
Throws an exception; objects of this class cannot be copied.
Table 4.234. Exceptions Thrown by FtpClient::copy()
err | desc |
|---|---|
| Objects of this class cannot be copied. |
Connects to the FTP server and attempts a login; if any errors occur, an exception is thrown.
This method does not need to be called explicitly; connections are established implicitly by all methdods that communicate with FTP servers.
$ftp.connect(); # connects to the URL set in the constructor
EVENT_CONNECTING, EVENT_CONNECTED,EVENT_HOSTNAME_LOOKUP, EVENT_HOSTNAME_RESOLVED, EVENT_START_SSL, EVENT_SSL_ESTABLISHED, EVENT_FTP_SEND_MESSAGE, EVENT_FTP_MESSAGE_RECEIVED, EVENT_PACKET_READ, EVENT_PACKET_SENT
Table 4.235. Exceptions Thrown by FtpClient::connect()
err | desc |
|---|---|
| Invalid response received from FTP server. |
| Cannot establish connection on data port, no hostname set, FTP server reported an error, etc. |
| Login denied by FTP server. |
Disconnects from an FTP server.
$ftp.disconnect();
Make an FTPS connection to the server on the next connect. This method can only be called before a connection is established; if called when a connection has already been established, an exception will be thrown.
nothing FtpClient::setSecure(softbool $secure = True)
$ftp.setSecure();
Table 4.236. Arguments for FtpClient::setSecure()
|
Argument |
Description |
|---|---|
|
if |
Table 4.237. Exceptions thrown by FtpClient::setSecure()
|
err |
desc |
|---|---|
|
|
cannot be called while a control connection has already been established. |
Make a non-encrypted connection to the server on the next connect.
$ftp.setInsecure();
Table 4.238. Exceptions thrown by FtpClient::setInsecure()
|
err |
desc |
|---|---|
|
|
cannot be called while a control connection has already been established. |
Make a non-encrypted data connection to the server on the next connect even if the control connection is secure.
$ftp.setInsecureData();
Table 4.239. Exceptions thrown by FtpClient::setInsecureData()
|
err |
desc |
|---|---|
|
|
cannot be called while a control connection has already been established. |
Returns True if the control connection is a secure TLS/SSL connection.
my bool $b = $ftp.isSecure();
Table 4.240. Return Values for FtpClient::isSecure()
|
Return Type |
Description |
|---|---|
|
True if the control connection is encrypted, |
Returns True if the data connections are secure TLS/SSL connections.
my bool $b = $ftp.isDataSecure();
Table 4.241. Return Values for FtpClient::isDataSecure()
|
Return Type |
Description |
|---|---|
|
True if data connections are encrypted, |
Returns the name of the cipher for an encrypted connection.
my *string $str = $ftp.getSSLCipherName();
Returns the version string of the cipher for encrypted connections.
my *string $str = $ftp.getSSLCipherVersion();
Returns a string code giving the result of verifying the remote certificate for secure (FTPS) connections. Return value are the keys described in the X509_VerificationReasons hash constant. This hash can also be used to generate a textual description of the verification result.
my *string $str = $ftp.verifyPeerCertificate();
Table 4.244. Return Values for FtpClient::verifyPeerCertificate()
|
Return Type |
Description |
|---|---|
|
A string code giving the result of verifying the peer's certificate. No value is returned if a secure connection has not been established. |
Sets the object to automatically try to negotiate the data connections in EPSV, PASV, and PORT modes, in this order.
$ftp.setModeAuto();
Sets the object to only try to make data connections using EPSV (RFC-2428 extended passive) mode.
$ftp.setModeEPSV();
Sets the object to only try to make data connections using PASV (RFC-959 passive) mode.
$ftp.setModePASV();
Sets the object to only try to make data connections using PORT mode.
$ftp.setModePORT();
Gets a string giving the list of files from the FTP server in the server's long format in the current working directory; returns NOTHING if the path cannot be found.
*string FtpClient::list(string $path)
my *string $str = $ftp.list();
EVENT_FTP_SEND_MESSAGE, EVENT_FTP_MESSAGE_RECEIVED, EVENT_PACKET_READ, EVENT_PACKET_SENT
Table 4.245. Arguments for FtpClient::list()
Argument | Description |
|---|---|
| The optional path or argument (filter) to list. |
Table 4.247. Exceptions Thrown by FtpClient::list()
err | desc |
|---|---|
| Incomplete message received on control port. |
| FTP server returned an error in response to the LIST command. |
Gets a list of file names in the current working directory from the FTP server.
*string FtpClient::nlst(string $path)
my *string $str = $ftp.nlst();
EVENT_FTP_SEND_MESSAGE, EVENT_FTP_MESSAGE_RECEIVED, EVENT_PACKET_READ, EVENT_PACKET_SENT
Table 4.248. Arguments for FtpClient::nlst()
Argument | Description |
|---|---|
| The optional path or argument (filter) to list. |
Table 4.250. Exceptions Thrown by FtpClient::nlst()
err | desc |
|---|---|
| Incomplete message received on control port. |
| FTP server returned an error in response to the NSLT command. |
Returns the server-side current working directory.
my string $str = $ftp.pwd();
EVENT_FTP_SEND_MESSAGE, EVENT_FTP_MESSAGE_RECEIVED, EVENT_PACKET_READ, EVENT_PACKET_SENT
Table 4.251. Return Values for FtpClient::pwd()
Return Type | Description |
|---|---|
The server-side current working directory. |
Table 4.252. Exceptions Thrown by FtpClient::pwd()
err | desc |
|---|---|
| Incomplete message received on control port. |
| FTP server returned an error to the PWD command. |
Changes the current working directory on the server.
nothing FtpClient::cwd(string $dir)
$ftp.cwd("/pub/gnu");
EVENT_FTP_SEND_MESSAGE, EVENT_FTP_MESSAGE_RECEIVED, EVENT_PACKET_READ, EVENT_PACKET_SENT
Table 4.253. Arguments for FtpClient::cwd()
Argument | Description |
|---|---|
| The directory to change to. |
Table 4.254. Exceptions Thrown by FtpClient::cwd()
err | desc |
|---|---|
| Incomplete message received on control port. |
| FTP server returned an error to the CWD command. |
Gets a file from the FTP server and stores it on the local system. If any errors occur, an exception is thrown.
This method accesses the filesystem, so it is not available if PO_NO_FILESYSTEM is set.
nothing FtpClient::get(string $remote_file)
nothing FtpClient::get(string $remote_file, string $local_file)
$ftp.get("file.txt", "/tmp/file-1.txt");
Not available with PO_NO_FILESYSTEM
EVENT_FTP_SEND_MESSAGE, EVENT_FTP_MESSAGE_RECEIVED, EVENT_PACKET_READ, EVENT_PACKET_SENT
Table 4.256. Exceptions Thrown by FtpClient::get()
err | desc |
|---|---|
| Incomplete message received on control port. |
| Could not create the local file. |
| There was an error retrieving the file. |
Gets a file from the FTP server and returns the file's contents as a binary value. For a similar function returning the file's contents as a string, see FtpClient::getAsString(); for a function that will get a remote file and save it on the local filesystem, see FtpClient::get().
binary FtpClient::getAsBinary(string $remote_file)
my binary $b = $ftp.getAsBinary("file.bin");
EVENT_FTP_SEND_MESSAGE, EVENT_FTP_MESSAGE_RECEIVED, EVENT_PACKET_READ, EVENT_PACKET_SENT
Table 4.257. Arguments for FtpClient::getAsBinary()
Argument | Description |
|---|---|
| The path on the server to the file to get. |
Table 4.258. Return Values for FtpClient::getAsBinary()
Return Type | Description |
|---|---|
The file retrieved; if any errors occur an exception is raised. |
Table 4.259. Exceptions Thrown by FtpClient::getAsBinary()
err | desc |
|---|---|
| Incomplete message received on control port. |
| Could not create the local file. |
| There was an error retrieving the file. |
Gets a file from the FTP server and returns the file's contents as a string. For a similar function returning the file's contents as a binary, see FtpClient::getAsBinary(); for a function that will get a remote file and save it on the local filesystem, see FtpClient::get().
string FtpClient::getAsString(string $remote_file)
my string $str = $ftp.getAsString("file.txt");
EVENT_FTP_SEND_MESSAGE, EVENT_FTP_MESSAGE_RECEIVED, EVENT_PACKET_READ, EVENT_PACKET_SENT
Table 4.260. Arguments for FtpClient::getAsString()
Argument | Description |
|---|---|
| The path on the server to the file to get. |
Table 4.261. Return Values for FtpClient::getAsString()
Return Type | Description |
|---|---|
The file retrieved; if any errors occur an exception is raised. |
Table 4.262. Exceptions Thrown by FtpClient::getAsString()
err | desc |
|---|---|
| Incomplete message received on control port. |
| Could not create the local file. |
| There was an error retrieving the file. |
Transfers a file to the FTP server. If any errors occur, an exception is thrown.
This method accesses the filesystem, so it is not available if PO_NO_FILESYSTEM is set.
int FtpClient::put(string $local_file)
int FtpClient::put(string $local_file, string $remote_file)
$ftp.put("/tmp/file-1.txt", "file.txt");
Not available with PO_NO_FILESYSTEM
EVENT_FTP_SEND_MESSAGE, EVENT_FTP_MESSAGE_RECEIVED, EVENT_PACKET_READ, EVENT_PACKET_SENT
Table 4.264. Exceptions Thrown by FtpClient::put()
err | desc |
|---|---|
| Incomplete message received on control port. |
| Could not open local file for reading. |
| Could not determine file size of local file (stat() failed). |
| An error occurred while sending the file. |
Transfers a data to the FTP server and saves it as a file on the remote machine; if any errors occur, an exception is thrown.
nothing FtpClient::putData(data $file_data, string $remote_file_name)
$ftp.putData($str, "file.txt");
EVENT_FTP_SEND_MESSAGE, EVENT_FTP_MESSAGE_RECEIVED, EVENT_PACKET_READ, EVENT_PACKET_SENT
Table 4.266. Exceptions Thrown by FtpClient::putData()
err | desc |
|---|---|
| Incomplete message received on control port. |
| An error occurred while sending the file. |
Deletes a file on the FTP server; if any errors occur, an exception is thrown.
nothing FtpClient::del(string $remote_file)
$ftp.del("file-2.txt");
EVENT_FTP_SEND_MESSAGE, EVENT_FTP_MESSAGE_RECEIVED, EVENT_PACKET_READ, EVENT_PACKET_SENT
Table 4.267. Arguments for FtpClient::del()
Argument | Description |
|---|---|
| The path on the server to the file to delete. |
Table 4.268. Exceptions Thrown by FtpClient::del()
err | desc |
|---|---|
| Incomplete message received on control port. |
| FTP server returned an error to the DELE command. |
Creates a directory on the remote FTP server; if any errors occur, an exception is thrown.
nothing FtpClient::mkdir(string $remote_dir)
$ftp.mkdir("tmp");
EVENT_FTP_SEND_MESSAGE, EVENT_FTP_MESSAGE_RECEIVED, EVENT_PACKET_READ, EVENT_PACKET_SENT
Table 4.269. Arguments for FtpClient::mkdir()
Argument | Description |
|---|---|
| The path on the server to the directory to create. |
Table 4.270. Exceptions Thrown by FtpClient::mkdir()
err | desc |
|---|---|
| Incomplete message received on control port. |
| FTP server returned an error to the MKD command. |
Removes a directory on the remote FTP server; if any errors occur, an exception is thrown.
nothing FtpClient::rmdir(string $remote_dir)
$ftp.rmdir("tmp");
EVENT_FTP_SEND_MESSAGE, EVENT_FTP_MESSAGE_RECEIVED, EVENT_PACKET_READ, EVENT_PACKET_SENT
Table 4.271. Arguments for FtpClient::rmdir()
Argument | Description |
|---|---|
| The path on the server to the directory to delete. |
Table 4.272. Exceptions Thrown by FtpClient::rmdir()
err | desc |
|---|---|
| Incomplete message received on control port. |
| FTP server returned an error to the RMD command. |
Renames/moves a file or directory; if any errors occur, an exception is thrown.
nothing FtpClient::rename(string $from, string $to)
$ftp.rename("file.txt", "file.txt.orig");
EVENT_FTP_SEND_MESSAGE, EVENT_FTP_MESSAGE_RECEIVED, EVENT_PACKET_READ, EVENT_PACKET_SENT
Table 4.274. Exceptions Thrown by FtpClient::rename()
err | desc |
|---|---|
| Incomplete message received on control port. |
| FTP server returned an error to the RNFR or RNTO commands. |
Sets the username for logging in to the FTP server.
nothing FtpClient::setUserName(string $user)
$ftp.setUserName("ftp");
Table 4.275. Arguments for FtpClient::setUserName()
Argument | Description |
|---|---|
| The username to use when logging in to the FTP server. |
Sets the login password for the FTP server to connect to.
nothing FtpClient::setPassword(string $pass)
$ftp.setPassword("ftp");
Table 4.276. Arguments for FtpClient::setPassword()
Argument | Description |
|---|---|
| The password to use when logging in to the FTP server. |
Sets the hostname value for the FTP server to connect to.
nothing FtpClient::setHostName(string $host)
$ftp.setHostName("hostname");
Table 4.277. Arguments for FtpClient::setHostName()
Argument | Description |
|---|---|
| The hostname to use when connecting to the FTP server. |
Sets the control port value (default is 21).
nothing FtpClient::setPort(softint $port)
$ftp.setPort(21);
Table 4.278. Arguments for FtpClient::setPort()
Argument | Description |
|---|---|
| The port to use when connecting to the FTP server. |
Table 4.279. Exceptions Thrown by FtpClient::setPort()
err | desc |
|---|---|
| Illegal port number passed as an argument (<= 0). |
Sets the connection and login parameters based on the URL passed as an argument.
nothing FtpClient::setURL(string $url)
$ftp.setURL("ftps://user:pass@host");
Table 4.280. Arguments for FtpClient::setURL()
Argument | Description |
|---|---|
| The URL to use to set connection and login parameters for the FTP server. |
Table 4.281. Exceptions Thrown by FtpClient::setURL()
err | desc |
|---|---|
| Only "ftp" is allowed as the protocol in the URL. |
| No hostname given in the URL. |
Retrieves the current username.
my *string $user = $ftp.getUserName();
Table 4.282. Return Values for FtpClient::getUserName()
Return Type | Description |
|---|---|
The current username value. |
Retrieves the current login password value for this object.
my *string $pass = $ftp.getPassword();
Table 4.283. Return Values for FtpClient::getPassword()
Return Type | Description |
|---|---|
The current password value. |
Retrieves the current hostname value for this object.
my *string $host = $ftp.getHostName();
Table 4.284. Return Values for FtpClient::getHostName()
Return Type | Description |
|---|---|
The current hostname value. |
Retrieves the current connection port value for this object.
my int $port = $ftp.getPort();
Table 4.285. Return Values for FtpClient::getPort()
Return Type | Description |
|---|---|
The current connection port value. |
Retrieves the current connection URL string for this object.
my string $url = $ftp.getURL();
Sets a Queue object to receive socket and FtpClient events on both the data and control connections.
To remove the event queue and stop monitoring socket events, pass NOTHING to the method. See Event Handling for more information.
To control monitoring of network events on just the data connection or just the control connection, use FtpClient::setDataEventQueue or FtpClient::setControlEventQueue respectively
nothing FtpClient::setEventQueue()
nothing FtpClient::setEventQueue(Queue $queue)
$ftp.setEventQueue($queue);
Sets a Queue object to receive socket and FtpClient events on the data connection when retrieving data from an FTP server.
To remove the event queue and stop monitoring network events, pass NOTHING to the method. See Event Handling for more information.
To control monitoring of network events on just the control connection or on both connections with the same queue, use FtpClient::setControlEventQueue or FtpClient::setEventQueue respectively.
nothing FtpClient::setDataEventQueue()
nothing FtpClient::setDataEventQueue(Queue $queue)
$ftp.setDataEventQueue($queue);
Sets a Queue object to receive socket and FtpClient events on both the control connection.
To remove the event queue and stop monitoring network events, pass NOTHING to the method. See Event Handling for more information.
To control monitoring of network events on just the data connection or for both data and control connections simultaneously, use FtpClient::setDataEventQueue or FtpClient::setEventQueue respectively
$ftp.setControlEventQueue($queue);