W3docs

filesize()

The filesize() function is a built-in PHP function that returns the size of a file in bytes. This function returns the file size as an integer.

What Is the filesize() Function?

filesize() is a built-in PHP function that returns the size of a file, measured in bytes, as an integer. It is the standard way to find out how much disk space a file occupies before you download it, display it, validate an upload, or decide whether to read it into memory.

This page covers the syntax, what the return value means, the all-important gotcha around cached results, how to turn raw bytes into a human-readable string, and how to handle missing or unreadable files safely.

Syntax

filesize(string $filename): int|false
  • $filename — Path to the file. It can be a relative path, an absolute path, or a URL wrapper (e.g. http://, ftp://) where the protocol supports reporting a size.
  • Return value — The size of the file in bytes as an integer, or false on failure (for example, the file does not exist or you lack permission to access it).

Because the size is in bytes, a file reported as 2048 is 2 KB, and 1048576 is exactly 1 MB.

A Basic Example

The example below creates a small temporary file, writes a known string to it, and reports its size. Writing the file first means the example is fully self-contained and runnable:

<?php

$filename = 'example.txt';
file_put_contents($filename, 'Hello, world!'); // 13 bytes

$bytes = filesize($filename);
echo "The file '$filename' is $bytes bytes.";
// The file 'example.txt' is 13 bytes.

The string Hello, world! is 13 characters of ASCII, so the file is exactly 13 bytes. For text files in a single-byte encoding, the byte count equals the character count; multi-byte (UTF-8) characters count as more than one byte.

filesize() works on the file as it sits on disk. It does not count bytes in memory, and for symbolic links it reports the size of the target file.

Handling a Missing or Unreadable File

If the file does not exist, filesize() returns false and emits a warning. Always check that the file exists first so you can fail gracefully:

<?php

$filename = 'does-not-exist.txt';

if (file_exists($filename)) {
    echo filesize($filename) . " bytes";
} else {
    echo "File not found.";
}
// File not found.

Because false loosely equals 0, never write if (filesize($f) == 0) to test for an empty file without first confirming the file exists — a missing file and an empty file would look identical. See file_exists() for the safe pre-check.

The Caching Gotcha (clearstatcache)

PHP caches the result of filesystem functions such as filesize(), filemtime(), and stat() for performance. If a file changes during the same script run, filesize() may return the old size. Call clearstatcache() to force a fresh read:

<?php

$filename = 'log.txt';
file_put_contents($filename, 'first');
echo filesize($filename) . "\n"; // 5

file_put_contents($filename, 'first-second');
clearstatcache(); // discard the cached size
echo filesize($filename) . "\n"; // 12

Without the clearstatcache() call, the second echo could still print 5. This is the single most common source of "wrong" filesize() results.

Converting Bytes to a Human-Readable Size

Raw byte counts are hard to read. A small helper converts them to KB, MB, GB, and so on:

<?php

function humanFilesize(int $bytes, int $decimals = 2): string
{
    $units = ['B', 'KB', 'MB', 'GB', 'TB'];
    $factor = (int) floor((strlen((string) $bytes) - 1) / 3);
    $value = $bytes / (1024 ** $factor);

    return number_format($value, $decimals) . ' ' . $units[$factor];
}

echo humanFilesize(13);        // 13.00 B
echo "\n";
echo humanFilesize(2048);      // 2.00 KB
echo "\n";
echo humanFilesize(1048576);   // 1.00 MB

Here number_format() handles the decimal places and 1024 ** $factor divides by the right power of 1024. Swap in round() if you prefer a numeric result without thousands separators.

When Would I Use filesize()?

  • Validating uploads — reject files larger than a limit before saving them (pair it with is_uploaded_file()).
  • Setting download headers — send a correct Content-Length header so browsers can show a progress bar.
  • Deciding how to read a file — for a huge file, stream it with fopen()/fread() instead of loading it all with file_get_contents().
  • Monitoring — track whether a log file is growing as expected.

Summary

filesize() returns a file's size in bytes, or false on failure. Always confirm the file exists first, remember to call clearstatcache() when a file changes mid-script, and convert bytes to a friendly unit with a helper when you display the result to users.

Practice

Practice
What is the purpose of the filesize() function in PHP?
What is the purpose of the filesize() function in PHP?
Was this page helpful?