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!
My team mate Edna Piranha is not only an awesome hacker; she's also a fantastic philosopher! Communication and online interactions is a subject that has kept her mind busy for a long time, and it has also resulted in a bunch of interesting experimental projects...
David asked me if I'd be up for a guest post picking out some of my favorite Pens from CodePen. A daunting task! There are so many! I managed to pick a few though that have blown me away over the past few months. If you...
The prospect of creating graphics charts with JavaScript is exciting. It's also the perfect use of JavaScript -- creating non-essential features with unobtrusive scripting. I've created a mix of PHP (the Analytics class), HTML, and MooTools JavaScript that will connect to Google Analytics...
CSS is a complete conundrum; we all appreciate CSS because of its simplicity but always yearn for the language to do just a bit more. CSS has evolved to accommodate placeholders, animations, and even click events. One problem we always thought...
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