Welcome back to shortlearner.com . Today we will see how to check the size of specified file.
PHP provide us an inbuilt function that is filesize() function.
the filesize()
Advertisement
false on failure.
Syntax
1 | filesize(filename) |
Example
1 2 3 4 5 6 | <?php $file = '/path/to/your/file'; $filesize = filesize($file); echo "The size of your file is $filesize bytes."; ?> |
filesize() function returns size of the file in bytes default.
for Converting bytes into kilobytes(KB) works by dividing the value by 1024.
PHP is very accurate and it will give us 12 decimal digits ,To avoid this we can make use of the
round() function and specify how many digits of accuracy we’d like to display in our output.
1 2 3 4 5 6 7 8 | <?php $file = '/path/to/your/file'; $filesize = filesize($file); // bytes $filesize = round($filesize / 1024, 2); // kilobytes with two digits echo "The size of your file is $filesize KB."; ?> |
1 2 3 4 5 6 7 | <?php $file = '/path/to/your/file'; $filesize = filesize($file); // bytes $filesize = round($filesize / 1024/1024, 1); // kilobytes with two digits echo "The size of your file is $filesize MB."; ?> |