Skip to content

Core\FileTransfer\Connection::write

Uploads bytes to a file on a server and returns the number of bytes written.

The content can be a bytes value, an open Core\IO\File, an uploaded file, the request body, or chunks such as an array of bytes. A file or chunks are sent piece by piece, so a large file never has to fit in memory. A file that is there already is replaced. A folder that does not exist throws an IOError.

By default the write is atomic (all or nothing). write first writes a file <name>.<16 hex digits>.part in the same folder. When the last byte is written, that file is renamed to the name you gave. So a partner never reads half a file. If the write fails, the .part file is deleted. With false as the third argument, write writes to the file directly. Use that only for a server that does not allow renames.

This works the same way over SFTP, FTPS, FTP and a directory block (a folder on disk that replaces the server).

Parameters

  • $pathstringThe path, relative to the block's root, such as in/orders.csv.
  • $contentCore\Bytes::Source|Core\Bytes::ChunksThe bytes to write. It can be a bytes value, an open Core\IO\File or an uploaded file. It can also be the request body, or chunks such as an array of bytes values.
  • $atomicbooldefault trueWith true, the content is written to a file <name>.<16 hex digits>.part in the same folder first. When all of it is written, that file is renamed to $path. So nobody reads half a file. If the write fails, the part file is deleted. With false, the content is written to $path directly.

Return value

Returns uint. The number of bytes written.

Errors

  • RuntimeErrorThe path starts with /, has an empty part or a .. part, or has a NUL, CR or LF character.
  • IOErrorThe folder does not exist, the server does not allow the write, or the server does not answer.
  • LogicErrorThe connection is closed.

Changelog

VersionChange
0.0.6Core\FileTransfer\Connection lists, reads, writes, moves and removes files on the server, with the method names of Core\IO. write first writes a temporary .part file and then renames it, so the server never has half a file. readStream reads a file in chunks of at most 64 KiB, so a large file is never in memory at once.

Examples

The examples below run with this configuration file.

nvs.toml
# The `partner` block reads and writes the folder `partner` beside this file. It needs no server.
[file_transfer.partner]
directory = "partner"
# The program may open the `partner` block.
[capabilities.file_transfer]
connect = ["partner"]
# A `directory` block also needs the right to read and write its folder.
# The folder `exports` has a local file that one example uploads.
[capabilities.fs]
read = ["partner", "exports"]
write = ["partner"]

Upload a report

<?nvs
// Shows how to upload a short report to a partner's server.
use Core\FileTransfer;
// `open` takes the name of the block `[file_transfer.partner]` in `nvs.toml`.
var $partner = FileTransfer::open('partner');
// `write` returns the number of bytes it wrote. This prints "wrote 21 bytes".
bytes $report = "day,total\n1,120\n2,95\n" as bytes;
echo 'wrote ', $partner->write('outbox/sales.csv', $report), " bytes\n";
// The file is now on the server.
echo $partner->readBytes('outbox/sales.csv') as string;
// The example deletes the file again, so it runs the same way each time.
$partner->remove('outbox/sales.csv');
Output
wrote 21 bytes
day,total
1,120
2,95

Replace a file safely

<?nvs
// Shows how `write` replaces a file, so that the partner never reads half of it.
use Core\Arr;
use Core\FileTransfer;
use Core\FileTransfer\Entry;
use Core\Str;
var $partner = FileTransfer::open('partner');
$partner->write('stock/today.csv', "sku,count\nMUG-01,12\n" as bytes);
// The second write replaces the file. `write` first writes the bytes to a file that ends in `.part`.
// When all bytes are written, that file gets the name `today.csv`.
$partner->write('stock/today.csv', "sku,count\nMUG-01,11\n" as bytes);
echo $partner->readBytes('stock/today.csv') as string;
// No `.part` file is left in the folder. This prints "readme.txt, today.csv".
array<tainted string> $names = [];
foreach ($partner->list('stock') as Entry $entry) {
$names[] = $entry->name();
}
echo Str::join(Arr::sort($names), ', '), "\n";
// With `false` as the third argument, `write` writes to `today.csv` directly.
// Use `false` only for a server that does not allow a file to be renamed.
echo 'wrote ', $partner->write('stock/today.csv', "sku,count\nMUG-01,10\n" as bytes, false), " bytes\n";
$partner->remove('stock/today.csv');
Output
sku,count
MUG-01,11
readme.txt, today.csv
wrote 20 bytes

Upload a local file

<?nvs
// Shows a nightly script that uploads a file from the local disk to a partner's server.
use Core\FileTransfer;
use Core\IO;
use Core\IO\File;
var $partner = FileTransfer::open('partner');
// `write` takes an open file and reads it in chunks. The whole file is never in memory at once.
File $local = IO::open('exports/invoices.csv', IO::FileMode::Read);
uint $sent = $partner->write('outbox/invoices.csv', $local);
$local->close();
// This prints "sent 40 bytes" and the size the server has.
echo 'sent ', $sent, " bytes\n";
echo 'on the server: ', $partner->metadata('outbox/invoices.csv')->size() ?? 0, " bytes\n";
$partner->remove('outbox/invoices.csv');
Output
sent 40 bytes
on the server: 40 bytes