Core\Storage::put
Core\Storage::put(
string $disk,
string $key,
Core\Bytes::Source|Core\Bytes::Chunks $contents,
{overwrite?: bool},
): voidSaves 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 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.$contentsCore\Bytes::Source|Core\Bytes::ChunksThe data to save:bytes, an openCore\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 istrue. Withfalse,putthrows anIOErrorif 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 aroot. Or$keyis not a valid key. Or thefs.readorfs.writecapability does not include the file's path.IOErrorThe file could not be written. This includesoverwrite: falsefor a key that is already used.
Examples
The examples below run with this configuration file.
# 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');milkbreadKeep 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');not saved: the key is already usedfirst versionSave 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);avatar-42.png: 11 bytes