How to get the size of a file in PHP?
Use the filesize() function. It returns the size of a file in bytes. If the file does not exist, it gives a warning and returns false.
<?php
file_put_contents("sample.txt", "Hello PHP");
echo filesize("sample.txt") . " bytes";
?>
Output:
Tutorials Class - Output Window
9 bytes
To show the size in KB, divide it by 1024.
<?php
$kb = filesize("sample.txt") / 1024;
echo round($kb, 2) . " KB";
?>
Output:
Tutorials Class - Output Window
0.01 KB
PHP remembers the result of filesize() for the same file. If the file changes during the same script, call clearstatcache() first.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |