Skip to content

Core\Storage::list

Core\Storage::list(string $disk, {prefix?: string}): array<string>Core\Storage::list(
    string $disk,
    {prefix?: string},
): array<string>

Returns the keys of the files on a disk, sorted.

The disk is a [storage.<name>] block in nvs.toml. On a server, a disk outside the application folder needs the fs.read capability for its folder. A program you start from the command line needs no capability. get can read every key in the result. With the prefix option, list returns only the keys that start with that text, for example every key that starts with invoice-.

Good to know: only files with a valid key are in the result. A folder, a symlink and a file whose name starts with a . are not. The keys are sorted by their bytes, so B comes before a.

Parameters

  • $diskstringThe name of a [storage.<name>] block in nvs.toml. That block sets the folder the files are saved in. A tainted value is not allowed here, because the program chooses the disk and user input never does.

Options

  • prefixstringoptionalOnly keys that start with this text are returned. The default is empty, which returns every key. A prefix that is not empty and not a valid key throws an error, because no key can start with it.

Return value

Returns array<string>. The matching keys, sorted. An empty array if no file matches. Only regular files with a valid key are listed. A folder, a symlink and a file with any other name are not in the result.

Errors

  • RuntimeErrorNo [storage.<name>] block with this name sets a root. Or prefix is not empty and not a valid key. Or the fs.read capability does not include the disk's folder.
  • IOErrorThe disk's folder could not be read. It does not exist, or it is not a folder.

Examples

The examples below run with this configuration file.

nvs.toml
# The `files` disk reads its files from the `disk` folder next to these examples.
# The path is relative to the folder of this file.
[storage.files]
root = "disk"
# The program may read files in that folder, and nowhere else.
# A path in `[capabilities]` is relative to this file.
[capabilities.fs]
read = ["disk"]

List every file

<?nvs
// Lists the key of every file on the `files` disk.
// The keys are sorted. This prints the two invoices, and then the receipt.
// The folder also has a `.keep` file. A key cannot start with a `.`, so it is not listed.
foreach (Core\Storage::list('files') as string $key) {
echo $key, "\n";
}
Output
invoice-1001.pdf
invoice-1002.pdf
receipt-17.txt

Only the invoices

<?nvs
// Uses the `prefix` option to list only the invoices.
array<string> $invoices = Core\Storage::list('files', { prefix: 'invoice-' });
// This prints `2 invoices: invoice-1001.pdf, invoice-1002.pdf`.
echo Core\Arr::count($invoices), ' invoices: ', Core\Str::join($invoices, ', '), "\n";
Output
2 invoices: invoice-1001.pdf, invoice-1002.pdf

Links for a download page

<?nvs
// Builds the links of a download page from the files on the disk.
final class Downloads
{
public static function links(): array<string>
{
array<string> $links = [];
foreach (Core\Storage::list('files') as string $key) {
$links[] = "<a href=\"/downloads/{$key}\">{$key}</a>";
}
return $links;
}
}
// This prints one link for each of the three files.
foreach (Downloads::links() as string $link) {
echo $link, "\n";
}
Output
<a href="/downloads/invoice-1001.pdf">invoice-1001.pdf</a>
<a href="/downloads/invoice-1002.pdf">invoice-1002.pdf</a>
<a href="/downloads/receipt-17.txt">receipt-17.txt</a>