Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

filesize

Returns the size of a file in bytes.

Syntax

filesize(string $filename): int|false

Parameters

ParameterTypeDescription
$filenamestringThe path to the file.

Return value

int|falseThe size in bytes, or `false` on failure.

Examples

The size in bytes and kilobytes:

<?php
 
$file = sys_get_temp_dir() . '/ref-size.txt';
file_put_contents($file, str_repeat('a', 2048));
 
echo filesize($file), ' ', round(filesize($file) / 1024, 1), ' KB';

Output:

2048 2 KB

Notes

PHP caches the result. If the file changed while the program was running, call clearstatcache() before asking again.

See also

  • file_exists — Checks whether a file or folder exists.
  • number_format — Formats a number for display: decimals and a thousands separator.

Related recipes

In the tutorial

  • Folders and paths — Working with folders in PHP — creating them, listing files, searching by a mask, parts of a path and file information.

Thanks for reading!

Donate to the site — we work hard for you.