Core\FileTransfer\Entry::size
$entry->size(): ?uintReturns the size of a file on the server, in bytes.
$entry->size() returns a uint, or null. A folder's size is null. The size is also null when the server does not send it. Most servers send the size in the listing, so size() sends no extra request to the server. Use ?? 0 or check for null before you calculate with it.
The entry keeps the size from the moment list, walk or metadata read it. When the file changes after that, call metadata again for the new size.
You can use the size to continue an upload that stopped. Read the size with metadata($path)->size(). Then call append to add the rest of the bytes to the end of the file.
The examples below print the size of each file in a folder, read the size before and after a change, and continue an upload that stopped.
Return value
Returns ?uint. The size, or null when the server does not send it. A folder's size is null.
Changelog
| Version | Change |
|---|---|
0.0.6 | list and walk return one Core\FileTransfer\Entry for each file or folder, and it has the same methods as Core\IO\Entry. Its size, modifiedAt and permissions methods return null when the server does not send the value. Every string it returns is tainted: the server chose it, so the methods of the connection do not accept it until your program checks it. |
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, and read and change the files in its folder.[capabilities.file_transfer]connect = ["partner"]
[capabilities.fs]read = ["partner"]write = ["partner"]The size of each file
<?nvs// Shows the size of each file in a folder on the server.
var $partner = Core\FileTransfer::open('partner');
// `size()` returns `null` for a folder, so `??` prints "no size" for "archive".// The first line is "2026-01.csv: 25".foreach ($partner->list('reports') as Core\FileTransfer\Entry $entry) { echo $entry->name(), ': ', $entry->size() ?? 'no size', "\n";}2026-01.csv: 252026-02.csv: 25archive: no sizeThe size after a change
<?nvs// Shows that an entry keeps the size from the moment the program read it.
var $partner = Core\FileTransfer::open('partner');$partner->write('upload.csv', "id,total\n" as bytes);var $before = $partner->metadata('upload.csv');
// The file gets longer after the first `metadata` call. `$before` still has the old size.$partner->append('upload.csv', "1,30\n" as bytes);
// A second `metadata` call reads the new size into `$after`.var $after = $partner->metadata('upload.csv');
// This prints "before: 9" and "after: 14".echo 'before: ', $before->size() ?? 0, "\n";echo 'after: ', $after->size() ?? 0, "\n";
$partner->remove('upload.csv');before: 9after: 14Resume an upload
<?nvs// Shows how a program continues an upload that stopped before the end.
var $partner = Core\FileTransfer::open('partner');string $report = "month,total\n2026-03,1410\n";
// The first upload stopped after 12 bytes. So the file on the server has only the line "month,total".$partner->write('2026-03.csv', Core\Str::slice($report, 0, 12) as bytes);
// The next run reads the size. `append` adds only the bytes after it to the end of the file.uint $done = $partner->metadata('2026-03.csv')->size() ?? 0;echo 'continue at byte ', $done, "\n";$partner->append('2026-03.csv', Core\Str::slice($report, $done as int) as bytes);
// This prints the whole report: "month,total" and "2026-03,1410".echo $partner->readBytes('2026-03.csv') as string;
$partner->remove('2026-03.csv');continue at byte 12month,total2026-03,1410