Generate Readable Byte Labels Using PHP
Whenever you manage disk space, it's infinitely easier to read when when the bytes are displayed in KB, MB, GB... format. When reading files on the disk, the server returns the disk space in bytes so it's on us programmers to program file sizes for display. Using PHP, this task is cake.
function format_bytes($bytes)
{
$labels = array('B','KB','MB','GB','TB');
for($x = 0; $bytes >= 1024 && $x < (count($labels) - 1); $bytes /= 1024, $x++);
return(round($bytes, 2).' '.$labels[$x]);
}
Users will appreciate this!
CSS animations aren't just for basic fades or sliding elements anymore -- CSS animations are capable of much more. I've showed you how you can create an exploding logo (applied with JavaScript, but all animation is CSS), an animated Photo Stack, a sweet...
The HTML5 revolution has provided us some awesome JavaScript and HTML APIs. Some are APIs we knew we've needed for years, others are cutting edge mobile and desktop helpers. Regardless of API strength or purpose, anything to help us better do our job is a...
My favorite web technology is quickly becoming the WebSocket API. WebSocket provides a welcomed alternative to the AJAX technologies we've been making use of over the past few years. This new API provides a method to push messages from client to server efficiently...
Thomas Fuchs, creator of script2 (scriptaculous' second iteration) and Zepto.js (mobile JavaScript framework), creates outstanding animated elements with JavaScript. He's a legend in his own right, and for good reason: his work has helped to inspire developers everywhere to drop Flash and opt...
I always like using the right-shift operator for this kind of stuff. So instead of:
$bytes /= 1024
I would use:
$bytes >>= 10
Of course, you lost the digits after the decimal point (unless you get fancy), but on the plus side, it’s way faster than floating point division. The shift operators must be some of the loneliest operators in PHP, don’t you think?
Nice one!
I did a similar function … somewhat overkill, but a usefull thing over the years =)
http://openminds.lucido-media.de/human-readable-bytes-sorry-php-net