Snipe.Net - Geeky Stuff

Displaying file sizes in human readable format

closeThis post was published 9 years 7 months 1 day ago. There is a chance that some APIs or software versions discussed have changed since this article was written.

This code prints out “b” for bytes, “m” for megs, etc depending on the file size.

= 1073741824) {
		$fileweight = round($fileweight / 1073741824 * 100) / 100 . "GB";
	} elseif ($fileweight >= 1048576) {
		$fileweight = round($fileweight / 1048576 * 100) / 100 . "MG";
	} elseif ($fileweight >= 1024) {
		$fileweight = round($fileweight / 1024 * 100) / 100 . "k";
	} else {
		$fileweight = $fileweight . "b";
	}
	return $fileweight;
} // end function
?> 

Also check out:

If you think this article kicked ass, subscribe to the RSS feed or follow me on Twitter! Share with your friends, or leave a comment below (or better still, do both!) My entire concept of self-worth is in your hands, so that makes you kind of a big deal. Srsly.

This entry was posted on Thursday, June 27th, 2002 at 2:28 pm and is filed under PHP/mySQL. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.