filesize() Function is one of the PHP operations used in filesystem and stream workflows. Returns the size of a file in bytes. The examples below show what it does and how to handle its result.
What Is filesize() Function?
Returns the size of a file in bytes.
Its syntax is:
filesize(string $filename)
The parameters are:
string $filename— The source or destination filesystem path.
Return value: The declared return type is int|false. Cached file stat data and files that change while being read need care.
Important: Cached file stat data and files that change while being read need care.
Example 1: Basic Usage
The basic example focuses on the behavior of filesize() Function: returns the size of a file in bytes. Inputs are defined locally or shown as application-controlled paths.
<?php
$fileSize = filesize(__FILE__);
if ($fileSize !== false) { echo $fileSize . " bytes"; }
The displayed value, file content or return status depends on the input and surrounding environment. Results involving local paths, identifiers or generated files are not fixed values.
Example 2: A Different Use Case
This second example illustrates another input or execution condition for filesize() Function. Notice the different checks, arguments, or output handling.
<?php
clearstatcache(true, __FILE__);
$length = filesize(__FILE__);
if ($length !== false) { echo number_format($length); }
This variation matters because cached file stat data and files that change while being read need care.
Practical Example: Using the Operation in an Application
The following example demonstrates how filesize() Function fits into a more complete file or archive workflow, including inputs and appropriate checks.
<?php
$reportFile = __DIR__ . "/report.pdf";
if (!is_file($reportFile)) { throw new RuntimeException("Missing report"); }
clearstatcache(true, $reportFile);
$bytes = filesize($reportFile);
if ($bytes === false || $bytes > 10 * 1024 * 1024) { throw new RuntimeException("Size out of limits"); }
The operation shown is an illustrative script. Adapt the filenames, required permissions, and error handling to your own controlled application environment before running it.
Important Behavior and Edge Cases
- Function behavior: Returns the size of a file in bytes.
- Limitation: Cached file stat data and files that change while being read need care.
Comparison With Related Functions
| Function or Method | Difference |
|---|---|
filesize() Function | Returns the size of a file in bytes. |
strlen() | Counts bytes in a string; filesize() checks size metadata for a filesystem path. |
Use filesize() Function for the specific behavior described above. The related operation, strlen(), serves a different purpose; do not substitute it unless that different behavior is intended.
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Assuming filesystem metadata is always available and current. | Cached file stat data and files that change while being read need care. Check failure returns and refresh stat information when files may have changed. |
| Treating strlen() as an interchangeable replacement for filesize() Function. | Counts bytes in a string; filesize() checks size metadata for a filesystem path. |
Key Takeaways
- Returns the size of a file in bytes.
- Cached file stat data and files that change while being read need care.
- Related operation: strlen() — Counts bytes in a string; filesize() checks size metadata for a filesystem path.
filesize() Function is useful when its specific behavior matches your application requirement. Keep paths controlled, check failures, and handle environment-dependent operations deliberately.