Skip to content

Core\FileTransfer\Connection::readBytes

$connection->readBytes(string $path): tainted bytes$connection->readBytes(
    string $path,
): tainted bytes

Downloads a whole file from a server and returns its content as tainted bytes.

The content is tainted (data from outside your program), because the server sent it. Convert it with as string to read it as text. A file that does not exist throws an IOError.

The path throws a RuntimeError when it starts with /, or has an empty part or a .. part. A part is the text between two /. A NUL, CR or LF character in the path also throws a RuntimeError. In these cases, nothing is sent to the server.

readBytes gets the size of the file first. Each request has a memory limit. When the file is larger than the memory the request can still use under this limit, readBytes throws an IOError. It throws before it reads a byte. Use readStream for such a file. It reads the file in small chunks.

This works the same way over SFTP, FTPS, FTP and a directory block (a folder on disk that replaces the server). Every transfer over FTP and FTPS is binary, so the bytes are exactly the bytes on the server.

Parameters

  • $pathstringThe path, relative to the block's root, such as in/orders.csv.

Return value

Returns tainted bytes. The content. The server sent it, so it is tainted (data from outside your program).

Errors

  • RuntimeErrorThe path starts with /, has an empty part or a .. part, or has a NUL, CR or LF character.
  • IOErrorThe file does not exist, or the server does not answer. readBytes also throws this error when the file is larger than the memory the request can still use under its memory limit. It throws before it reads any byte. Use readStream for such a file.
  • LogicErrorThe connection is closed.

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 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 also needs the right to read its folder.
[capabilities.fs]
read = ["partner"]

Download a text file

<?nvs
// Shows how to download a whole file from a partner's server.
use Core\Bytes;
use Core\FileTransfer;
// `open` takes the name of the block `[file_transfer.partner]` in `nvs.toml`.
var $partner = FileTransfer::open('partner');
// `readBytes` returns the content as `tainted bytes`, because the server sent it.
// A tainted value comes from outside the program, and it must be checked before some uses.
tainted bytes $content = $partner->readBytes('inbox/welcome.txt');
echo Bytes::length($content), " bytes\n";
// `as string` converts the bytes to text. This prints "Hello from the partner.".
echo $content as string;
Output
24 bytes
Hello from the partner.

A missing file throws

<?nvs
// Shows the error `readBytes` throws for a file that does not exist.
use Core\FileTransfer;
var $partner = FileTransfer::open('partner');
// `readBytes` throws an `IOError`. The program catches the error and prints its message.
// Then the program continues with the next line.
try {
$partner->readBytes('inbox/returns.csv');
} catch (IOError $missing) {
echo $missing->message, "\n";
}
echo "The import continues with the next file.\n";
Output
Core\FileTransfer\Connection::readBytes: nothing exists at "inbox/returns.csv"
The import continues with the next file.

Import the orders of the day

<?nvs
// Shows a nightly script that downloads a CSV file of orders and adds up the totals.
use Core\Arr;
use Core\Csv;
use Core\FileTransfer;
var $partner = FileTransfer::open('partner');
tainted string $text = $partner->readBytes('inbox/orders.csv') as string;
// `Csv::parse` with `header: true` reads the first line as the column names.
// Each row then has these column names as keys.
var $rows = Csv::parse($text, { header: true });
int $sum = 0;
foreach ($rows as var $row) {
// `as ?int` converts the text to a whole number. If that is not possible, the result is `null`.
$sum = $sum + ($row['total'] as ?int ?? 0);
}
// This prints "3 orders, 87 in total".
echo Arr::count($rows), ' orders, ', $sum, " in total\n";
Output
3 orders, 87 in total