Most developer languages give you a file's size in bytes. With these methods you can convert them to readable amounts and units.
echo friendly_filesize(45056).'<br />'; // 44 kB echo friendly_filesize(1490944).'<br />'; // 1.4 MB echo friendly_filesize(24022183936); // 22.4 GB function friendly_filesize($bytes) { $measurements = array('TB'=>1099511627776, 'GB'=>1073741824, 'MB'=>1048576, 'kB'=>1024, 'b'=>1); foreach($measurements as $key=>$value) { $conv = $bytes/$value; if($conv > 1) { return round($conv, 1)." $key"; } } }
<script type="text/javascript"> function friendly_filesize(bytes) { var labels = new Array('TB', 'GB', 'MB', 'kB', 'b'); var measurements = new Array(1099511627776, 1073741824, 1048576, 1024, 1); for(var i=0; i<measurements.length; i++) { var conv = bytes/measurements[i]; if(conv > 1) { return Math.round(conv*10)/10+' '+labels[i]; } } } </script> <p id="filesize"><script type="text/javascript">document.write(friendly_filesize(24022183936));</script></p>
No comments yet
What's on your mind?
Gravatar