Human-readable file size

Tuesday, May 01, 2007   —   Vienna 0 Comments

Logarithm to change file size string like this 1234567 one to human-readable format like 123 MB.

function formatSize($s)
{
	$units = array("Bytes", "KB", "MB", "GB", "TB");

	$count = count($units);
	for($i = 0; $i < $count && $s >= 1024; $i++) { $s /= 1024; }

	return (round($s, 2) . " " . $units[$i]);
}

Add a comment

The fields that are highlighted contain errors
Name:
Email:     (Not shown)
Website:
Comment:
  Notify me when my comment has been cleared.
  Notify me of followup comments.