Skip to content

Core\Storage::get

Reads a file from a disk by its key, and returns its bytes.

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. If no file has the key, get returns null. This is not an error, so you can handle a missing file with ?? or == null. A key that is not valid, such as one with a /, throws an error.

The result is tainted bytes. A file on a disk is often an upload, so its bytes need the same checks as a value from a request before they reach HTML, a query or a path.

Good to know: get reads the whole file into memory. To see which files exist without reading them, use Core\Storage::list.

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.
  • $keystringThe name of the file on the disk. It is 1 to 255 ASCII letters, digits, ., - and _, and it does not start with a .. A / or a \ throws an error, so a key never points into another folder.

Return value

Returns ?tainted bytes. The file's bytes, or null if nothing was saved under this key. The bytes are tainted, because a visitor may have written the file.

Errors

  • RuntimeErrorNo [storage.<name>] block with this name sets a root. Or $key is not a valid key. Or the fs.read or fs.write capability does not include the file's path.
  • IOErrorThe file is there, but it could not be read.

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"]

Read a saved file

<?nvs
// Reads a file that is already on the `files` disk.
// The result is `tainted`, because a visitor may have written the file.
?tainted bytes $welcome = Core\Storage::get('files', 'welcome.txt');
// The file exists, so the result is its bytes. This prints `Welcome to the shop!`.
echo ($welcome ?? '' as bytes) as string;
Output
Welcome to the shop!

A missing file is null

<?nvs
// Shows that `get` returns `null` when no file has the key.
?tainted bytes $logo = Core\Storage::get('files', 'logo.png');
// No file has this key. This prints `no logo yet`.
if ($logo == null) {
echo "no logo yet\n";
}
Output
no logo yet

Load settings with a default

<?nvs
// Loads the shop's settings from a saved file, and uses default settings when the file is missing.
final class Settings
{
public static function load(string $key): tainted string
{
bytes $defaults = '{"currency": "USD", "perPage": 10}' as bytes;
// `??` replaces a `null` result with the default settings.
tainted bytes $saved = Core\Storage::get('files', $key) ?? $defaults;
return $saved as string;
}
}
// `settings.json` is on the disk. This prints the saved settings.
echo Settings::load('settings.json'), "\n";
// `old-settings.json` is not on the disk. This prints the default settings.
echo Settings::load('old-settings.json'), "\n";
Output
{"currency": "EUR", "perPage": 20}
{"currency": "USD", "perPage": 10}