PHP – Format File Size

The given PHP function returns the size of file in KB, MB or GB instead bytes.

Code Example:
<?php 
function format_filesize ($fsize) { 
	if (strlen($fsize) <= 9 && strlen($fsize) >= 7) {                  
		$fsize = number_format($fsize / 1048576,1); 
		return "$fsize MB"; 
	} elseif (strlen($fsize) >= 10) { 
		$fsize = number_format($fsize / 1073741824,1); 
		return "$fsize GB"; 
	} else { 
		$fsize = number_format($fsize / 1024,1); 
		return "$fsize KB"; 
	} 
} 
?>

Post to Twitter Post to Digg Post to Facebook Post to Google Buzz Send Gmail

Leave a Comment

Your email address will not be published. Required fields are marked *