Home > Article > Backend Development > PHP gets file size and converts gb, mb, kb code
This is a very smart code that uses php program to get the size of the file and displays the gb, mb, kb code according to the file size. The code is as follows:
function sizecount($filesize) {
if( $filesize >= 1073741824) {
$filesize = round($filesize / 1073741824 * 100) / 100 . ' gb';
} elseif($filesize >= 1048576) {
$filesize = round($ filesize / 1048576 * 100) / 100 . ' mb';
} elseif($filesize >= 1024) {
$filesize = round($filesize / 1024 * 100) / 100 . ' kb';
} else {//Open source code phpfensi.com
$filesize = $filesize . ' bytes';
}
return $filesize;
}
//Application method
$fenpath='./phpsi .com/logo.gif';
echo sizecount(filesize($path));