Skip to content

Core\IO::readBytes

Core\IO::readBytes(string $path): bytesCore\IO::readBytes(
    string $path,
): bytes

Reads the whole content of a file as bytes, exactly as it is on disk.

Use Core\IO::readBytes for a file that is not text, such as an image, a PDF or a ZIP archive. Nothing is checked or changed, so every byte of the file is in the result. An empty file returns empty bytes. To read UTF-8 text as a string, use Core\IO::read. For text in another character set, use Core\IO::readText.

readBytes throws an IOError when the file does not exist, is a folder, or cannot be read. It throws a RuntimeError 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, readBytes 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 bytes. The file's content as bytes, with nothing changed.

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.
  • 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 the size of a png image

<?nvs
// Shows how to read an image file as bytes and find its width and height.
// This writes the first 24 bytes of a PNG image that is 640 by 480 pixels.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'photo.png');
Core\IO::write($path, Core\Encoding::fromHex('89504e470d0a1a0a0000000d4948445200000280000001e0'));
// `readBytes` returns the file exactly as it is on disk. This prints "24 bytes".
bytes $image = Core\IO::readBytes($path);
echo Core\Bytes::length($image), " bytes\n";
// Every PNG file starts with the same 8 bytes. This prints "png: yes".
bytes $signature = Core\Encoding::fromHex('89504e470d0a1a0a');
echo 'png: ', Core\Bytes::startsWith($image, $signature) ? 'yes' : 'no', "\n";
// The width and height are two 4-byte numbers at position 16. This prints "640 x 480".
array<mixed> $size = Core\Bytes::unpack(Core\Bytes::slice($image, 16, 8), 'NN');
echo ($size[0] as uint) as string, ' x ', ($size[1] as uint) as string, "\n";
Output
24 bytes
png: yes
640 x 480

A file that is not utf 8

<?nvs
// Shows how to read a file that is not UTF-8 text.
// In the Latin-1 character set, "é" is the single byte e9. That byte alone is not valid UTF-8.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'export.csv');
Core\IO::write($path, Core\Encoding::fromHex('6361e9'));
// `Core\IO::read` needs UTF-8 text, so it throws an error. This prints "not UTF-8".
try {
echo Core\IO::read($path);
} catch (RuntimeError $e) {
echo "not UTF-8\n";
}
// `readBytes` does not check the content. This prints "6361e9".
bytes $content = Core\IO::readBytes($path);
echo Core\Encoding::toHex($content), "\n";
Output
not UTF-8
6361e9

Back up an archive

<?nvs
// Shows how to make a backup copy of a binary file and check that it is exact.
string $dir = Core\IO::createTemporaryDirectory();
string $original = Core\Path::join($dir, 'invoices.zip');
string $backup = Core\Path::join($dir, 'invoices.zip.bak');
// This writes the start of a ZIP archive. Byte ff and byte 00 are not text.
Core\IO::write($original, Core\Encoding::fromHex('504b0304140000000800ff00'));
// `write` takes `bytes` too, so the copy has the same bytes as the original.
Core\IO::write($backup, Core\IO::readBytes($original));
// The two SHA-256 digests are equal only when every byte is equal. This prints "backup ok".
bytes $before = Core\Hash::of(Core\IO::readBytes($original), Core\Hash::Digest::Sha256);
bytes $after = Core\Hash::of(Core\IO::readBytes($backup), Core\Hash::Digest::Sha256);
echo $before == $after ? 'backup ok' : 'backup differs', "\n";
Output
backup ok