PHP filesize() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, filesize() returns the size of a file in bytes.

It is a quick way to check how big an uploaded file is, to show a download size, or to decide how much data to read before you open the file.

Syntax

Call filesize() with the values you need. Start with this line.

PHP
$bytes = filesize('notes.txt');
CallWhat it does
filesize('notes.txt')Returns the size of notes.txt in bytes, such as 2048.
filesize('empty.txt')Returns 0 for an empty file, which is a valid result and not an error.
round(filesize('notes.txt') / 1024, 1)Converts the size from bytes to kilobytes.

filesize() works on files, and on folders the value depends on the system, so it is not a reliable folder size. PHP caches the size, so call clearstatcache() when the same file is written to and measured again during one script.

Library declarations
PHP
filesize(string $filename): int|false

Parameters

ParameterDescription
filenameRequired. The path to the file you want to measure.

Return Value

Return ValueDescription
intReturns the size of the file in bytes.
falseReturns false on failure, for example when the file does not exist.

PHP Examples

Example 1 — Get the Size of a File

Write a short message to a file and print how many bytes it uses.

PHP
<?php
$file = __DIR__ . '/size_demo.txt';
file_put_contents($file, 'Hello, CodeToFun!');

$bytes = filesize($file);

echo "File size: $bytes bytes", PHP_EOL;

unlink($file);

How It Works

  1. file_put_contents() writes the text Hello, CodeToFun! to the file.
  2. The text has 17 characters, and each plain letter uses one byte.
  3. filesize() reads the size from the file system and returns 17.
  4. echo prints the size inside the message.

Example 2 — A Missing File

filesize() returns false when the file cannot be found, so check before you use the value.

PHP
<?php
$file = __DIR__ . '/missing.txt';

$bytes = @filesize($file);

if ($bytes === false) {
    echo "Could not get the file size", PHP_EOL;
} elseif ($bytes === 0) {
    echo "The file is empty", PHP_EOL;
} else {
    echo "Size: $bytes bytes", PHP_EOL;
}

How It Works

  1. The file does not exist, so filesize() returns false.
  2. The @ sign hides the warning so the script can show its own message.
  3. A second branch handles an existing empty file, which returns 0 and not false.
  4. Using === keeps the two cases apart.

Example 3 — Show a Friendly Size

Write a helper that turns bytes into KB or MB so the size is easy to read.

PHP
<?php
function formatSize(int $bytes): string {
    if ($bytes >= 1048576) {
        return round($bytes / 1048576, 1) . ' MB';
    }
    if ($bytes >= 1024) {
        return round($bytes / 1024, 1) . ' KB';
    }
    return $bytes . ' bytes';
}

$file = __DIR__ . '/big.txt';
file_put_contents($file, str_repeat('A', 2048));

echo formatSize(filesize($file)), PHP_EOL;

unlink($file);

How It Works

  1. str_repeat() creates a text of 2048 letters, which is exactly 2048 bytes.
  2. filesize() returns 2048 for that file.
  3. formatSize() divides by 1024 because the number is at least one kilobyte.
  4. round() trims the result, and 2. 0 is shown as 2 in the final text.

Frequently Asked Questions

It returns the size of a file in bytes.
Divide the byte count by 1024 for KB, or by 1048576 for MB, and use round() to keep the number short.
PHP caches file information during the request. Call clearstatcache() before filesize() when the file was changed in the same script.
It returns false and raises a warning. Check with file_exists() first or compare the result with === false.
Not reliably. It returns a system dependent value for folders. To get a folder size, loop through the files and add up their sizes.

Next: filetype()

filetype() tells you what kind of item a path points to, such as a file, a directory, or a.

filetype() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful