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 innvs.toml. That block sets the folder the files are saved in. Ataintedvalue 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 aroot. Orprefixis not empty and not a valid key. Or thefs.readcapability 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.
# 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";}invoice-1001.pdfinvoice-1002.pdfreceipt-17.txtOnly 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";2 invoices: invoice-1001.pdf, invoice-1002.pdfLinks 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";}<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>