PHP filesize bytes convert KB MB GB TB
Convert File Size to Human Readable Format in PHP. Converting bytes to human readable values (KB, MB, GB, TB, PB) PHP function
1 Byte = 8 Bits
1 Kilobyte = 1024 Bytes
1 Megabyte = 1048576 Bytes
1 Gigabyte = 1073741824 Bytes
1 Terabyte = 1099511627776 bytes
function
<?php function formatBytes($bytes) { if ($bytes > 0) { $i = floor(log($bytes) / log(1024)); $sizes = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); return sprintf('%.02F', round($bytes / pow(1024, $i),1)) * 1 . ' ' . @$sizes[$i]; } else { return 0; } } ?>
usage
<?php echo formatBytes('1000'); // 1000B echo formatBytes('1073741824'); //1GB ?>
<?php $file_size = filesize('myfile.zip'); // Get file size echo '<div title="'.$file_size.' bytes">'.formatBytes($file_size).'</div>'; //output //<div title="1073741824 bytes">1 GB</div> ?>