Skip to content

Core\FileTransfer\Connection::close

$connection->close(): void

Closes the connection now. You do not need to call close: the connection is closed when the request or the script ends. Call it when the program still has a lot of work to do after its last transfer.

After close, every method of the connection throws a LogicError. Closing a connection twice is not an error. To use the block again, call Core\FileTransfer::open once more. It returns a new connection.

For an SFTP, FTPS or FTP block, the network connection to the server stays open after close. The next Core\FileTransfer::open of the same block can use it again, without a new sign-in. This is not done when a download or an upload on the connection has not finished. Then close ends the network connection too. With pool = false in the block, close always ends it.

A directory block has no network connection. For it, close only makes the connection unusable.

Return value

Returns void. Nothing.

Changelog

VersionChange
0.0.6Core\FileTransfer\Connection lists, reads, writes, moves and removes files on the server, with the method names of Core\IO. write first writes a temporary .part file and then renames it, so the server never has half a file. readStream reads a file in chunks of at most 64 KiB, so a large file is never in memory at once.

Examples

The examples below run with this configuration file.

nvs.toml
# The `partner` block reads and writes the folder `partner` beside this file. It needs no server.
[file_transfer.partner]
directory = "partner"
# The program may open the `partner` block.
[capabilities.file_transfer]
connect = ["partner"]
# A `directory` block needs read and write access to its folder.
[capabilities.fs]
read = ["partner"]
write = ["partner"]

Close the connection when the work is done

<?nvs
// Shows how to close a connection as soon as the program no longer needs it.
var $partner = Core\FileTransfer::open('partner');
echo $partner->readBytes('readme.txt') as string;
// The program has what it needs, so it closes the connection now.
// The rest of the script can take a long time, and the connection is not kept open for it.
$partner->close();
echo "closed\n";
Output
Files for the partner.
closed

A closed connection throws a logicerror

<?nvs
// Shows that every method of a closed connection throws a `LogicError`.
var $partner = Core\FileTransfer::open('partner');
$partner->close();
// The connection is closed, so `exists` throws.
try {
$partner->exists('readme.txt');
} catch (LogicError $closed) {
echo $closed->message, "\n";
}
Output
Core\FileTransfer\Connection::exists: this connection is closed

Close twice and open again

<?nvs
// Shows that closing a connection twice is not an error, and that the block can be opened again.
var $first = Core\FileTransfer::open('partner');
$first->close();
// The second `close` does nothing.
$first->close();
echo "closed twice\n";
// `open` returns a new connection to the same block. This prints "readme.txt: yes".
var $second = Core\FileTransfer::open('partner');
echo 'readme.txt: ', $second->exists('readme.txt') ? 'yes' : 'no', "\n";
Output
closed twice
readme.txt: yes