Skip to content

Core\Request\Part::saveTo

$part->saveTo(string $path, {max?: uint, overwrite?: bool}): void$part->saveTo(
    string $path,
    {max?: uint, overwrite?: bool},
): void

Writes one uploaded file to a file on disk. It reads the upload in small pieces and writes each piece at once, so even a very large file uses little memory. A request may write only in the folders that Core\IO::createTemporaryDirectory made for it. Any other path needs the fs.write capability.

{ max: … } sets the largest size in bytes. Without it, there is no limit. A larger file throws a RuntimeError, and the part that was already written is deleted. saveTo does not replace a file that already exists and throws an error instead. { overwrite: true } allows it.

The file name from filename() is tainted, so saveTo does not accept it as a path. Pass it through Core\IO::within first. Call saveTo while the Core\Request::files() loop is on this file. After the loop moves on, it throws a LogicError. The content can be read only once.

The examples show how to save a file, what happens when it is too large, and how to store files under the names the client sent.

Parameters

  • $pathstringThe file to write the part to. It must not already exist, unless overwrite is true.

Options

  • maxuintoptionalThe most bytes to accept from this part. If you leave it out, there is no limit, because no other limit applies to a file on disk. readAll keeps what it reads in memory, so [limits] request_body limits it.
  • overwritebooloptionalWhether an existing file may be replaced. The default is false, because the path is usually built from a name the client sent.

Return value

Returns void. Nothing. If the write fails partway, the partial file is deleted before the error is thrown. No later reader finds a cut-off upload that the program thinks is complete.

Errors

  • LogicErrorThis program is not handling a request. Or the files() loop has moved to a later part, and the bytes of this part are gone.
  • RuntimeErrorThe configuration does not grant fs.write for this path, or the part is larger than max bytes.
  • IOErrorA file is already at the path and overwrite is false. Or the operating system did not allow the file to be created or written. Or the connection failed while the body was read.
  • ParseErrorThe body is not the multipart body the request declared.

Examples

Saving an upload to a file

<?nvs
// Shows how to write an uploaded file to disk.
string $folder = Core\IO::createTemporaryDirectory();
foreach (Core\Request::files() as Core\Request\Part $part) {
string $path = Core\Path::join($folder, 'report.txt');
// `saveTo` writes the file to `$path`. It keeps only a small piece in memory at a time.
$part->saveTo($path);
// This prints "saved 38 bytes" and then the content of the file.
echo 'saved ', Core\IO::size($path), " bytes\n";
echo Core\IO::readText($path), "\n";
}
Output
saved 38 bytes
Week 12: 48 orders, 2 returns, 0 late.

A file larger than max

<?nvs
// Shows what happens when an uploaded file is larger than the size you allow.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'video.mp4');
foreach (Core\Request::files() as Core\Request\Part $part) {
// `max` is the largest size in bytes. A larger file throws a `RuntimeError`.
try {
$part->saveTo($path, { max: 20 });
echo "the video was saved\n";
} catch (RuntimeError $tooLarge) {
echo "the video is larger than 20 bytes\n";
}
}
// The part that was already written is deleted. This prints "file exists: no".
echo 'file exists: ', Core\IO::exists($path) ? 'yes' : 'no', "\n";
Output
the video is larger than 20 bytes
file exists: no

Storing attachments under their own names

<?nvs
// Shows how a help desk stores the attachments of a ticket under the names the client sent.
string $attachments = Core\IO::createTemporaryDirectory();
foreach (Core\Request::files() as Core\Request\Part $part) {
try {
// The client chooses the file name, so `saveTo` does not accept it directly.
// `Core\IO::within` throws a `RuntimeError` for a name that leaves the folder.
string $path = Core\IO::within($attachments, $part->filename());
// A file larger than 10000000 bytes throws a `RuntimeError`.
$part->saveTo($path, { max: 10000000 });
echo 'stored ', $part->filename(), "\n";
} catch (RuntimeError $refused) {
echo 'refused ', $part->filename(), "\n";
}
}
Output
stored error-log.txt
refused ../../config/app.toml