Core\Storage::get
Core\Storage::get(string $disk, string $key): ?tainted bytesCore\Storage::get( string $disk, string $key, ): ?tainted bytes
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 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.$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 aroot. Or$keyis not a valid key. Or thefs.readorfs.writecapability 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.
# 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;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";}no logo yetLoad 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";{"currency": "EUR", "perPage": 20}{"currency": "USD", "perPage": 10}