How to check filesize using PHP.
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() function returns the size of the file in bytes on success or
false on failure.
Syntax
filesize(filename)
Example
<?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.
<?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."; ?>
To display the value in megabytes we’ll simply divide by 1024 twice:
<?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."; ?>