New to our community ?

Discover a world of possibilities! Join us and explore a vibrant community where ideas flourish and connections thrive.

One of Our Valued Members

Thank you for being part of our community. Your presence enriches our shared experiences. Let's continue this journey together!

Home Uncategorized How to check filesize using PHP.

How to check filesize using PHP.

0

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.

how to check file size in php
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.";
?>