Skip to content

Core\IO::read

Reads the whole content of a text file into one string.

Core\IO::read takes a path and returns everything in the file. Nothing is changed: line breaks, spaces and a line break at the end are all kept. An empty file returns an empty string.

The file must be UTF-8 text. For a file in another character set, use Core\IO::readText. To go through a large file one line at a time, use Core\IO::lines.

read throws an IOError when the file does not exist, is a folder, or cannot be read. It throws a RuntimeError when the file is not valid UTF-8, or when the file is larger than the [limits] max_output setting allows.

A program you start from the command line may read any path. On a server, a program may read only the files in its application folder, except nvs.toml and names that start with .. Any other path needs the fs.read capability. Without it, read throws a RuntimeError.

Parameters

  • $pathstringThe file to read. A relative path must be a string written directly in the code. It is joined to the folder of the file that contains it.

Return value

Returns string. The file's content as a string, with nothing stripped. The content must be UTF-8 text; readText reads a file written in another charset.

Errors

  • RuntimeErrorThe configuration does not grant fs.read for this path. The message names the capability the way nvs.toml writes it. Or the file is larger than [limits] max_output, the limit for a single read in a request. The same setting limits the captured output of a child process. Or the file is not valid UTF-8, and the message names the byte where the text stops.
  • IOErrorThe capability allowed the call, but the operating system did not. The file does not exist, is a directory, or could not be read.

Changelog

VersionChange
0.0.5When 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

Read a whole file

<?nvs
// Shows how to read the whole content of a text file into one string.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'notes.txt');
Core\IO::write($path, "Buy milk\nCall Ada\n");
// `read` returns the file exactly as it is, also the line break at the end.
// This prints the two lines, and then "18 characters".
string $notes = Core\IO::read($path);
echo $notes;
echo Core\Str::length($notes), " characters\n";
Output
Buy milk
Call Ada
18 characters

A file that is not there

<?nvs
// Shows what happens when the file to read does not exist.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'cache.txt');
// `read` throws an `IOError` when the file is missing. This prints "no cache yet".
try {
echo Core\IO::read($path);
} catch (IOError $e) {
echo "no cache yet\n";
}
// After the file is written, the same call returns its content. This prints "cached value".
Core\IO::write($path, "cached value\n");
echo Core\IO::read($path);
Output
no cache yet
cached value

Fill in an email template

<?nvs
// Shows how a program reads an email template from a file and fills in the customer's details.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'welcome.txt');
Core\IO::write($path, "Hello {name},\nyour order {order} is on its way.\n");
// `Core\Str::replaceAll` replaces each key in the text with its value.
array<string> $details = ['{name}' => 'Ada', '{order}' => '#1042'];
string $mail = Core\Str::replaceAll(Core\IO::read($path), $details);
// This prints "Hello Ada," and "your order #1042 is on its way."
echo $mail;
Output
Hello Ada,
your order #1042 is on its way.