Skip to content

Core\FileTransfer\Connection::list

$connection->list(string $folder = ""): Core\FileTransfer\Listing$connection->list(
    string $folder = "",
): Core\FileTransfer\Listing

Lists the files and folders in one folder of a server, without . and ...

You use the result in a foreach loop. Each item is a Core\FileTransfer\Entry with its name, its size, its modified time and whether it is a file or a folder. The folder is relative to the block's root, and an empty string is the root itself.

A path throws a RuntimeError when it starts with / or has a NUL, CR or LF character. A part of the path between two / that is empty or .. also throws. Then nothing is sent.

The items come in the order the server sends them. Sort the names when the order matters. On SFTP the size, the time and the permissions come with the list. An FTP or FTPS server sends them when it supports the MLSD command. Otherwise list reads the plain text list that the server sends. A value the server does not send is null.

Good to know: the names come from the server, so each one is a tainted string. A tainted value came from outside the program, and must be checked before some uses.

Parameters

  • $folderstringdefault ""The folder, relative to the block's root. An empty string is the root itself.

Return value

Returns Core\FileTransfer\Listing. One Core\FileTransfer\Entry per item, in the order the server sends them.

Errors

  • RuntimeErrorThe path starts with /, has an empty part or a .. part, or has a NUL, CR or LF character.
  • IOErrorThe folder does not exist, or the server does not answer.
  • 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"]

List the files in a folder

<?nvs
// Shows how to list the files and folders in one folder of a partner's server.
use Core\Arr;
use Core\FileTransfer;
use Core\FileTransfer\Entry;
use Core\Str;
// `open` takes the name of a block in `nvs.toml`. The block `[file_transfer.partner]` has the name `partner`.
var $partner = FileTransfer::open('partner');
// The server sends the names, so each name is a `tainted string`: it must be checked before some uses.
array<tainted string> $lines = [];
foreach ($partner->list('inbox') as Entry $entry) {
if ($entry->isDirectory()) {
$lines[] = $entry->name() . ' (folder)';
} else {
$lines[] = $entry->name() . ', ' . (($entry->size() ?? 0) as string) . ' bytes';
}
}
// The server chooses the order of the list, so the program sorts it.
echo Str::join(Arr::sort($lines), "\n"), "\n";
Output
done (folder)
orders-0412.csv, 19 bytes
orders-0413.csv, 14 bytes
readme.txt, 22 bytes

Count the csv files

<?nvs
// Shows how to count the CSV files in a folder and add up their sizes.
use Core\FileTransfer;
use Core\FileTransfer\Entry;
use Core\Str;
var $partner = FileTransfer::open('partner');
uint $files = 0;
uint $bytes = 0;
foreach ($partner->list('inbox') as Entry $entry) {
// The loop skips the folder `done` and the file `readme.txt`.
if (!$entry->isFile() || !Str::endsWith($entry->name(), '.csv')) {
continue;
}
$files = $files + 1;
uint $size = $entry->size() ?? 0;
$bytes = $bytes + $size;
}
// This prints "2 CSV files, 33 bytes".
echo $files, ' CSV files, ', $bytes, " bytes\n";
Output
2 CSV files, 33 bytes

Check that every file arrived

<?nvs
// Shows a nightly script that checks whether a partner sent every order file of the week.
use Core\Arr;
use Core\FileTransfer;
use Core\FileTransfer\Entry;
var $partner = FileTransfer::open('partner');
// The program lists the folder once and keeps the file names.
array<tainted string> $received = [];
foreach ($partner->list('inbox') as Entry $entry) {
if ($entry->isFile()) {
$received[] = $entry->name();
}
}
// This prints "received" for the first two files and "missing" for the third.
array<string> $week = ['orders-0412.csv', 'orders-0413.csv', 'orders-0414.csv'];
foreach ($week as string $expected) {
echo $expected, ': ', Arr::contains($received, $expected) ? 'received' : 'missing', "\n";
}
Output
orders-0412.csv: received
orders-0413.csv: received
orders-0414.csv: missing