Core\IO::append
Core\IO::append(string $path, string|bytes $content): voidCore\IO::append( string $path, string|bytes $content, ): void
Adds text or bytes to the end of a file. If the file does not exist, Core\IO::append creates it.
The content is a string or bytes, and append writes it exactly as it is. What the file already contains stays as it is, and the new content comes after it. This is the difference from Core\IO::write, which replaces the whole file. append adds no line break of its own, so end each line with "\n" yourself.
A program you start from the command line may write any path. On a server, a program may write only in the folders that Core\IO::createTemporaryDirectory made for it. Any other path needs the fs.write capability. Without it, append throws a RuntimeError and creates nothing. When the system cannot write the file, for example because the folder does not exist, append throws an IOError.
Good to know: the system puts every call at the end of the file at the moment it writes. Two programs that add lines to the same log file do not write over each other's lines.
Parameters
$pathstringThe file to add to. A relative path must be a string written directly in the code. It is joined to the folder of the file that contains it.$contentstring|bytesThe text or bytes to add. The file's current content is kept, and these bytes come after it. The operating system finds the end of the file when it writes. The file is not read first.
Return value
Returns void. Nothing. A failure throws an error. It never returns false, so a caller that ignores the result does not miss a failure.
Errors
RuntimeErrorThe configuration does not grantfs.writefor this path. This is checked before anything is created, so the failed append leaves no file behind.IOErrorThe capability allowed the call, but the operating system did not. A directory is missing, the filesystem is read-only, or the path is a directory.
Changelog
| Version | Change |
|---|---|
0.0.5 | When a request is recorded, each call to Core\IO::read, Core\IO::readText, Core\IO::readBytes, Core\IO::write and Core\IO::append is saved with its line and its file path. The editor shows the number of these calls and their time above the line. |
Examples
Add lines to a log file
<?nvs// Shows how to add lines to the end of a log file, one call for each line.
string $log = Core\Path::join(Core\IO::createTemporaryDirectory(), 'app.log');
// The file does not exist yet, so the first call creates it.Core\IO::append($log, "started\n");Core\IO::append($log, "loaded 3 settings\n");Core\IO::append($log, "stopped\n");
// This prints the three lines in the order they were added.echo Core\IO::read($log);startedloaded 3 settingsstoppedAppend keeps what write replaces
<?nvs// Shows the difference between `Core\IO::append` and `Core\IO::write` on a file that has text in it.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'note.txt');Core\IO::write($path, 'first');
// `append` keeps "first" and adds to the end. This prints "first, second".Core\IO::append($path, ', second');echo Core\IO::read($path), "\n";
// `write` replaces everything in the file. This prints "third".Core\IO::write($path, 'third');echo Core\IO::read($path), "\n";
// An empty text adds nothing. The file is still 5 bytes long.Core\IO::append($path, '');echo Core\IO::size($path), "\n";first, secondthird5An audit trail for orders
<?nvs// Shows an audit trail: each change to an order adds one CSV line to a file that is never rewritten.
class Audit{ public static function record(string $file, int $order, string $action, string $user): void { // `Core\Csv::format` gives one CSV line for each record. It puts quotes around a field that contains a comma. Core\IO::append($file, Core\Csv::format([[$order as string, $action, $user]])); }}
string $file = Core\Path::join(Core\IO::createTemporaryDirectory(), 'orders-audit.csv');
Audit::record($file, 1042, 'created', 'anna');Audit::record($file, 1042, 'paid', 'anna');Audit::record($file, 1042, 'shipped, partly', 'warehouse');
// This prints the three lines that were added, one for each change.echo Core\IO::read($file);1042,created,anna1042,paid,anna1042,"shipped, partly",warehouse