Core\FileTransfer\Connection::append
$connection->append(
string $path,
Core\Bytes::Source|Core\Bytes::Chunks $content,
): uintAdds bytes to the end of a file on the server. If the file does not exist, append creates it. It returns the number of bytes it wrote.
append takes the same content as write: a bytes value, an open Core\IO\File, an uploaded file, the request body, or an array of bytes values.
You can use append to continue an upload that stopped before its end. metadata($path)->size() returns the size of the file on the server. Then append sends only the bytes after that position.
On FTP and FTPS, append sends the APPE command, so the server must support it.
The folder of the file must exist. Otherwise append throws an IOError, and it creates nothing. It also throws an IOError when the server does not allow the write.
A part of a path is the text between two /. A path throws a RuntimeError when it starts with /, has an empty part or a .. part, or has a NUL, CR or LF character. Then nothing is sent.
Parameters
$pathstringThe path, relative to the block'sroot, such asin/orders.csv.$contentCore\Bytes::Source|Core\Bytes::ChunksThe bytes to write. It can be abytesvalue, an openCore\IO\Fileor an uploaded file. It can also be the request body, or chunks such as an array ofbytesvalues.
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
| Version | Change |
|---|---|
0.0.6 | Core\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.
# 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 needs read and write access to its folder.[capabilities.fs]read = ["partner"]write = ["partner"]Add lines to a log file
<?nvs// Shows how to add lines to the end of a file on a server.
var $partner = Core\FileTransfer::open('partner');
// The first `append` creates the file. Each call returns the number of bytes it wrote.// This prints "wrote 12" and then "wrote 13".echo 'wrote ', $partner->append('log-01.txt', "job started\n" as bytes), "\n";echo 'wrote ', $partner->append('log-01.txt', "job finished\n" as bytes), "\n";
// The file has both lines, in the order they were added.echo $partner->readBytes('log-01.txt') as string;
// This deletes the file, so the example can run again.$partner->remove('log-01.txt');wrote 12wrote 13job startedjob finishedContinue an upload that stopped
<?nvs// Shows how to continue an upload that stopped before its end.
var $partner = Core\FileTransfer::open('partner');bytes $report = "id,total\n1,30\n2,45\n3,12\n" as bytes;
// This upload stopped after the first 14 bytes.$partner->write('report-02.csv', Core\Bytes::slice($report, 0, 14));
// `metadata('report-02.csv')->size()` returns the size of the file on the server.// This prints "the server has 14 bytes".uint $sent = $partner->metadata('report-02.csv')->size() ?? 0;echo 'the server has ', $sent, " bytes\n";
// `append` sends only the bytes after that position. This prints "sent 10 more bytes".echo 'sent ', $partner->append('report-02.csv', Core\Bytes::slice($report, $sent as int)), " more bytes\n";
// The file on the server is now complete.echo $partner->readBytes('report-02.csv') as string;
$partner->remove('report-02.csv');the server has 14 bytessent 10 more bytesid,total1,302,453,12The folder must exist
<?nvs// Shows that the folder of the file must exist before `append` can write to it.
var $partner = Core\FileTransfer::open('partner');
// The folder `logs-03` does not exist, so `append` throws an `IOError`.try { $partner->append('logs-03/today.txt', "line\n" as bytes); echo "written\n";} catch (IOError $missing) { echo "the folder does not exist\n";}
// `append` did not create the folder. `exists` returns `false`, so this prints "logs-03: no".echo 'logs-03: ', $partner->exists('logs-03') ? 'yes' : 'no', "\n";
// `makeDirectory` creates the folder. Then `append` works.// The content can also be an array of `bytes` values. This prints "wrote 11".$partner->makeDirectory('logs-03');echo 'wrote ', $partner->append('logs-03/today.txt', ["first\n" as bytes, "next\n" as bytes]), "\n";
$partner->remove('logs-03/today.txt');$partner->removeDirectory('logs-03');the folder does not existlogs-03: nowrote 11