Core\FileTransfer::open
Core\FileTransfer::open(string $endpoint): Core\FileTransfer\ConnectionCore\FileTransfer::open( string $endpoint, ): Core\FileTransfer\Connection
Opens a connection to a file server. The server and the sign-in are set in a [file_transfer.<name>] block of nvs.toml. Your program passes only the name of the block.
A server block has host and protocol. The protocol is sftp (over SSH), ftps (FTP with TLS) or ftp (not encrypted). The name of the block must also be listed in [capabilities.file_transfer] connect.
A directory block uses a folder on disk in place of a server, for tests and local work. You write it as directory = "partner". It also needs [capabilities.fs] read for the folder, and write if your program changes files. The examples on this page use such a block.
open throws a RuntimeError in three cases:
- The name is not listed in
connect. - No block has that name.
- The block sets neither
protocolnordirectory.
It throws an IOError when the server cannot be reached, when the sign-in fails, or when the folder does not exist. If a TLS connection fails, the error names the cause and the fix (TLS connections).
Good to know: the connection closes when the request or the script ends.
Parameters
$endpointstringThe name of the block, such aspartnerfor[file_transfer.partner]. It must be listed in[capabilities.file_transfer] connect.
Return value
Returns Core\FileTransfer\Connection. The open connection.
Errors
RuntimeErrorThe name is not listed in[capabilities.file_transfer] connect, no block has that name, or the block sets noprotocoland nodirectory.IOErrorThe server cannot be reached or does not accept the sign-in, or thedirectoryof the block does not exist.
Changelog
| Version | Change |
|---|---|
0.0.6 | A failed TLS connection names its cause, who can fix it and how, as TLS connection errors explains. For an expired certificate, the message gives the date it expired and the time on this host's clock. For a wrong pin, it gives the sha256// hash of the server's public key. |
0.0.6 | Core\FileTransfer::open connects to a file server over SFTP, FTPS or FTP, with the server and the sign-in of a [file_transfer.<name>] block in nvs.toml. A program gives only the name of the block. The same code works with a directory block, which is a folder on disk that replaces the server in tests. |
Examples
The examples below run with this configuration file.
# The `partner` block reads the folder `partner` beside this file. It needs no server.# On a real server, the block has `protocol`, `host`, `user` and a password or key instead.[file_transfer.partner]directory = "partner"
# The `archive` block is not listed under `connect`, so the program may not open it.[file_transfer.archive]directory = "partner"
# The program may open `partner` and `backup`. No `[file_transfer.backup]` block exists.[capabilities.file_transfer]connect = ["partner", "backup"]
# A `directory` block also needs the right to read and write its folder.[capabilities.fs]read = ["partner"]write = ["partner"]Open a block and read a file
<?nvs// Shows how to open a connection, list the files in its folder and read one of them.
use Core\FileTransfer;use Core\FileTransfer\Entry;
// `open` takes the name of the block `[file_transfer.partner]` in `nvs.toml`.var $partner = FileTransfer::open('partner');
// `list` gives one entry for each file and folder. This prints "outbox/" and "prices.csv".foreach ($partner->list() as Entry $entry) { echo $entry->name(), $entry->isDirectory() ? '/' : '', "\n";}
// `readBytes` returns the whole file. This prints the three lines of `prices.csv`.echo $partner->readBytes('prices.csv') as string;outbox/prices.csvsku,priceA100,12.50B200,7.25The same code runs over a directory in a test
<?nvs// Shows a method that sends a report to a server, run over a folder on disk in a test.// In production, the block has `protocol = "sftp"` and the address of the server.// In this test, the block has `directory = "partner"`, so the same code writes to a folder.
use Core\FileTransfer;
class Reports{ // The method takes the name of the block. The same code works for a server and for a folder. public static function send(string $block, string $csv): uint { var $connection = FileTransfer::open($block); return $connection->write('outbox/report.csv', $csv as bytes); }}
// This prints "sent 14 bytes".echo 'sent ', Reports::send('partner', "id,total\n1,30\n"), " bytes\n";
// The test checks that the file is there. This prints "report.csv is there".var $check = FileTransfer::open('partner');echo $check->exists('outbox/report.csv') ? "report.csv is there\n" : "report.csv is missing\n";
// The test removes the file, so the folder is the same as before the test.$check->remove('outbox/report.csv');sent 14 bytesreport.csv is thereA block that cannot be opened
<?nvs// Shows the errors `open` throws for a block that the program cannot open.
use Core\FileTransfer;
// `[capabilities.file_transfer] connect` lists the blocks the program may open. It does not list `archive`.// So `open` throws a `RuntimeError`. This prints its message and a line that says which setting to add.try { FileTransfer::open('archive');} catch (RuntimeError $notGranted) { echo $notGranted->message, "\n";}
// `backup` is in the list, but `nvs.toml` has no `[file_transfer.backup]` block.// So `open` throws a `RuntimeError`, and this prints its message.try { FileTransfer::open('backup');} catch (RuntimeError $noBlock) { echo $noBlock->message, "\n";}Core\FileTransfer::open needs the capability `file_transfer.connect` for archive, which is not grantedhelp: add `connect = ["archive"]` under `[capabilities.file_transfer]` in nvs.tomlCore\FileTransfer::open: no `[file_transfer.backup]` block sets `protocol` or `directory`, so there is no connection of that name to open