Skip to content

Core\Storage::put

Saves data as a file with a name, on a disk that nvs.toml sets up.

The data can be bytes, an open file, an uploaded file, Core\Request::bodyStream(), or a list or loop that gives bytes pieces. A file or an upload is written one piece at a time. If reading the data fails, put deletes the file it started.

A disk is a [storage.<name>] block in nvs.toml. Its root is the folder that has the files, and on a server the fs.write capability must include that folder. A program you start from the command line needs no capability. The key is the name of the file. It can use ASCII letters, digits, ., - and _, and it is at most 255 bytes long. A key with a / or a \ throws an error, so a program cannot write outside the folder.

If a file already has the key, put replaces it. With overwrite: false, put saves the file only when the key is free. Otherwise it throws an IOError.

Good to know: the key may come from user input, but the disk name may not. You write the disk name in the code.

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.
  • $contentsCore\Bytes::Source|Core\Bytes::ChunksThe data to save: bytes, an open Core\IO\File, an uploaded file, the request body, or the pieces of a loop. A file or an upload is written one piece at a time, so it is never held in memory as a whole.

Options

  • overwritebooloptionalWhether a file that already has this key may be replaced. The default is true. With false, put throws an IOError if the key is already used.

Return value

Returns void. Nothing. The file is on the disk when put returns.

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 could not be written. This includes overwrite: false for a key that is already used.

Examples

The examples below run with this configuration file.

nvs.toml
# The `files` disk saves its files in 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 and write files in that folder, and nowhere else.
# A path in `[capabilities]` is relative to this file.
[capabilities.fs]
read = ["disk"]
write = ["disk"]

Save a note

<?nvs
// Saves a short text as a file on the `files` disk, and reads it back.
Core\Storage::put('files', 'shopping-list.txt', "milk\nbread\n" as bytes);
// `get` returns the saved bytes. This prints `milk` and `bread`.
tainted bytes $saved = Core\Storage::get('files', 'shopping-list.txt') ?? '' as bytes;
echo $saved as string;
// This deletes the file again, so the next run starts with an empty disk.
Core\Storage::delete('files', 'shopping-list.txt');
Output
milk
bread

Keep the first version

<?nvs
// Uses `overwrite: false` to save a file only when its key is not used yet.
Core\Storage::put('files', 'draft.txt', "first version\n" as bytes);
// The key is already used, so this `put` throws an `IOError`.
try {
Core\Storage::put('files', 'draft.txt', "second version\n" as bytes, { overwrite: false });
} catch (IOError $e) {
echo "not saved: the key is already used\n";
}
// The file still has the first version. This prints `first version`.
tainted bytes $saved = Core\Storage::get('files', 'draft.txt') ?? '' as bytes;
echo $saved as string;
Core\Storage::delete('files', 'draft.txt');
Output
not saved: the key is already used
first version

Save an uploaded picture

<?nvs
// Saves a profile picture under a key made from the user's id, as an upload form does.
final class Avatars
{
public static function save(int $userId, bytes $picture): string
{
string $key = "avatar-{$userId}.png";
Core\Storage::put('files', $key, $picture);
return $key;
}
}
// The second upload uses the same key, so it replaces the first picture.
string $key = Avatars::save(42, 'first picture' as bytes);
Avatars::save(42, 'new picture' as bytes);
// The disk has one file for this user. This prints `avatar-42.png: 11 bytes`.
tainted bytes $saved = Core\Storage::get('files', $key) ?? '' as bytes;
echo $key, ': ', Core\Bytes::length($saved), " bytes\n";
Core\Storage::delete('files', $key);
Output
avatar-42.png: 11 bytes