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!
![Responsive Images: The Ultimate Guide]()
Chances are that any Web designers using our Ghostlab browser testing app, which allows seamless testing across all devices simultaneously, will have worked with responsive design in some shape or form. And as today's websites and devices become ever more varied, a plethora of responsive images...
![5 Ways that CSS and JavaScript Interact That You May Not Know About]()
CSS and JavaScript: the lines seemingly get blurred by each browser release. They have always done a very different job but in the end they are both front-end technologies so they need do need to work closely. We have our .js files and our .css, but...
![Ana Tudor’s Favorite CodePen Demos]()
Cocoon
I love canvas, I love interactive demos and I don't think I have ever been more impressed by somebody's work than when I discovered what Tiffany Rayside has created on CodePen. So I had to start off with one of her interactive canvas pens, even though...
![prefers-color-scheme: CSS Media Query]()
One device and app feature I've come to appreciate is the ability to change between light and dark modes. If you've ever done late night coding or reading, you know how amazing a dark theme can be for preventing eye strain and the headaches that result.
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