Core\FileTransfer\Connection::readStream
$connection->readStream(
string $path,
uint $offset = 0,
): Core\FileTransfer\ChunksReads a file from a server in chunks of at most 64 KiB, so a large file never has to fit in memory.
You use the result in a foreach loop, or pass it to a method that takes chunks, such as Core\Csv::rows. Each chunk is tainted bytes (data from outside your program), because the server sent it. The second argument is the first byte to read. With it, you can continue a download that stopped, after the bytes you already have. When the offset is the size of the file, the loop gets no chunks and no error. So you can use the saved size as the offset, even when the earlier download was complete.
On SFTP, the offset is part of each read request. On FTP and FTPS, the download starts with the REST command (an FTP command that sets the first byte to send). A directory block (a folder on disk that replaces the server) reads the file from the offset.
Good to know: on FTP and FTPS, another call on the same connection during the loop ends the download. The next chunk then throws an IOError. On SFTP, the download continues.
Parameters
$pathstringThe path, relative to the block'sroot, such asin/orders.csv.$offsetuintdefault 0The first byte to read. Use it to continue a download that stopped.
Return value
Returns Core\FileTransfer\Chunks. The chunks. The server sent them, so they are tainted.
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. On FTP and FTPS, another call on the same connection during the loop ends the download. Then the next chunk throws this error.LogicErrorThe connection is closed.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\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.
# 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"]Read a file in chunks
<?nvs// Shows how to read a file from a partner's server in chunks.
use Core\Bytes;use Core\FileTransfer;
// `open` takes the name of the block `[file_transfer.partner]` in `nvs.toml`.var $partner = FileTransfer::open('partner');
// Each chunk has at most 64 KiB. This small file comes in one chunk.uint $chunks = 0;uint $bytes = 0;foreach ($partner->readStream('logs/access.log') as tainted bytes $chunk) { $chunks = $chunks + 1; $bytes = $bytes + Bytes::length($chunk);}
// This prints "chunks: 1, bytes: 71".echo 'chunks: ', $chunks, ', bytes: ', $bytes, "\n";chunks: 1, bytes: 71Continue a download that stopped
<?nvs// Shows how to continue a download that stopped in the middle of a file.
use Core\Bytes;use Core\FileTransfer;
var $partner = FileTransfer::open('partner');
// An earlier run saved the first 14 bytes, which are the first line. Then the connection stopped.uint $saved = 14;
// The second argument is the first byte to read. So this run reads only the rest of the file.// This prints the last three lines of the file, then "read 57 more bytes, 71 in total".uint $rest = 0;foreach ($partner->readStream('logs/access.log', $saved) as tainted bytes $chunk) { $rest = $rest + Bytes::length($chunk); echo $chunk as string;}echo 'read ', $rest, ' more bytes, ', $saved + $rest, " in total\n";GET /shop/cart 200POST /shop/order 201GET /missing 404read 57 more bytes, 71 in totalImport a large csv file
<?nvs// Shows an import script that reads a large CSV file one row at a time.
use Core\Csv;use Core\FileTransfer;
var $partner = FileTransfer::open('partner');
// `Csv::rows` takes the chunks and gives one row at a time.// Only one chunk is in memory, so the file can be larger than the memory of the program.uint $orders = 0;int $sum = 0;foreach (Csv::rows($partner->readStream('exports/orders.csv'), { header: true }) as var $row) { $orders = $orders + 1; // `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 "4 orders, 95 in total".echo $orders, ' orders, ', $sum, " in total\n";4 orders, 95 in total