filesize
Returns the size of a file in bytes.
filesize(string $filename): int|false- Added in PHP 4.0.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$filename | string | Path to the file. |
Return value
int|falseThe size of the file in bytes, or false on failure.
Errors
Emits an E_WARNING and returns false if the stat call fails, for example when the file does not exist or is not accessible.
filesize gives the byte length of a file without opening or reading it. It is a single stat() call, so it is cheap even on very large files.
When to reach for it
- Setting a correct
Content-Lengthheader before streaming a download. - Rejecting an upload that exceeds a size limit.
- Deciding whether a file is small enough to slurp with
file_get_contentsor must be streamed. - Detecting a zero-byte file left behind by a failed job.
What trips people up
An empty file returns 0, and a failure returns false. Both are falsy, so if (!filesize($f)) cannot tell "this file is empty" from "this file does not exist". Use === false for the error check.
The result is stat-cached. Write to a file and call filesize in the same request and you will very likely get the old size — clearstatcache() is the fix, and this catches people constantly.
The number is bytes, not characters. For a UTF-8 file with multibyte characters, filesize and mb_strlen of the contents will disagree. Directories do not have a meaningful size here either; the value you get back is filesystem bookkeeping, not the total size of the contents.
Example
When to use it
- Send an accurate Content-Length header before calling readfile on a download.
- Refuse to process an uploaded CSV larger than 10 MB.
- Alert when a nightly export file comes out as zero bytes.
Frequently asked questions
Why does filesize return the wrong size after I write to the file?
PHP caches stat results per request, so you are seeing the size from before the write. Call `clearstatcache(true, $filename)` between writing and reading the size.
How do I get the size of a directory with filesize?
You cannot — `filesize` on a directory returns a filesystem bookkeeping number, not the total of its contents. Recurse with `RecursiveDirectoryIterator` and sum `filesize()` for each file.
Does filesize count characters or bytes?
Bytes. A UTF-8 file where each emoji takes four bytes will report a larger number than the character count you would get from `mb_strlen()`.
More examples
The stat cache in action
The output reads `before: 5, stale: 5, fresh: 500`. The middle value is the bug people hit: PHP served the size it stat'ed before the write until the cache was cleared for that path.
<?php
file_put_contents('grow.txt', 'small');
$before = filesize('grow.txt');
file_put_contents('grow.txt', str_repeat('x', 500));
$stale = filesize('grow.txt'); // cached
clearstatcache(true, 'grow.txt');
$fresh = filesize('grow.txt');
echo "before: $before, stale: $stale, fresh: $fresh\n";
unlink('grow.txt');Human-readable sizes
The raw figure is 1049088 bytes; the helper renders it as `1 MB`. Dividing by 1024 repeatedly is the standard way to turn a byte count into something worth showing a user.
<?php
function human(int $bytes): string {
$units = ['B', 'KB', 'MB', 'GB'];
$i = 0;
while ($bytes >= 1024 && $i < count($units) - 1) {
$bytes /= 1024;
$i++;
}
return round($bytes, 2) . ' ' . $units[$i];
}
file_put_contents('report.bin', str_repeat('z', 1024 * 1024 + 512));
clearstatcache();
echo filesize('report.bin'), " bytes\n";
echo human(filesize('report.bin')), "\n";
unlink('report.bin');Distinguishing empty from missing
The output separates `exists but empty` from `could not stat`. A plain truthiness check would have collapsed both into the same branch, which is why the strict `=== false` comparison matters here.
<?php
file_put_contents('empty.dat', '');
clearstatcache();
foreach (['empty.dat', 'nope.dat'] as $path) {
$size = @filesize($path);
if ($size === false) {
echo "$path: could not stat\n";
} elseif ($size === 0) {
echo "$path: exists but empty\n";
} else {
echo "$path: $size bytes\n";
}
}
unlink('empty.dat');
Discussion