Skip to content

Core\IO::size

Returns the size of a file in bytes.

Core\IO::size returns the number of bytes in the file at the path, as a uint. The operating system gives this number, so the program does not read the file to get it.

The size counts bytes, not characters. Some characters take more than one byte. For example, "Café" has four characters and five bytes. To count the characters in a string, use Core\Str::length.

When there is no file at the path, size throws an IOError. Check with Core\IO::exists first when a missing file is a normal case.

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, size throws a RuntimeError.

Good to know: use size to reject a file that is too large before you read it into memory.

Parameters

  • $pathstringThe file to measure. 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 uint. The byte count as a uint. For a text file this counts bytes, not characters. Core\Str::length gives the length of a string in graphemes (what a person counts as one character).

Errors

  • RuntimeErrorThe configuration does not grant fs.read for this path.
  • IOErrorThe capability allowed the call, but the operating system did not. Nothing is at the path, or its metadata could not be read. A missing file has no size, so this method throws an error. exists returns false for it.

Examples

Bytes not characters

<?nvs
// Shows that `Core\IO::size` counts the bytes in a file, not the characters.
string $dir = Core\IO::createTemporaryDirectory();
string $file = Core\Path::join($dir, 'name.txt');
// "Cafe" has four characters and four bytes. This prints 4.
Core\IO::write($file, 'Cafe');
echo Core\IO::size($file), "\n";
// "Café" has four characters, but "é" takes two bytes. This prints 5.
Core\IO::write($file, 'Café');
echo Core\IO::size($file), "\n";
// An empty file has a size of 0. This prints 0.
Core\IO::write($file, '');
echo Core\IO::size($file), "\n";
Output
4
5
0

A missing file throws

<?nvs
// Shows that `Core\IO::size` throws an `IOError` when there is no file at the path.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'missing.txt');
// A missing file has no size, so `size` throws an error. This prints "no file to measure".
try {
echo Core\IO::size($path), "\n";
} catch (IOError $e) {
echo 'no file to measure', "\n";
}
// You can check with `exists` first. It returns `false`, so this prints "the file does not exist".
if (Core\IO::exists($path)) {
echo 'size: ', Core\IO::size($path), "\n";
} else {
echo 'the file does not exist', "\n";
}
Output
no file to measure
the file does not exist

Refuse an upload that is too big

<?nvs
// Shows how a program checks the size of uploaded files before it reads them.
class Uploads
{
// The largest file this program accepts, in bytes.
public const uint LIMIT = 1000;
public static function accept(string $path): string
{
uint $size = Core\IO::size($path);
if ($size > self::LIMIT) {
return 'too big (' . ($size as string) . ' bytes)';
}
return 'accepted (' . ($size as string) . ' bytes)';
}
}
string $dir = Core\IO::createTemporaryDirectory();
string $small = Core\Path::join($dir, 'avatar.png');
string $large = Core\Path::join($dir, 'video.mp4');
Core\IO::write($small, Core\Str::repeat('x', 300));
Core\IO::write($large, Core\Str::repeat('x', 5000));
// This prints "avatar.png: accepted (300 bytes)" and then "video.mp4: too big (5000 bytes)".
array<string> $uploads = [$small, $large];
foreach ($uploads as string $path) {
echo Core\Path::fileName($path), ': ', Uploads::accept($path), "\n";
}
Output
avatar.png: accepted (300 bytes)
video.mp4: too big (5000 bytes)