Skip to content

Instantly share code, notes, and snippets.

@Kcko
Forked from liunian/gist:9338301
Created October 7, 2016 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kcko/74c6cfb3e890e1fe5dbee17328a31c3d to your computer and use it in GitHub Desktop.
Save Kcko/74c6cfb3e890e1fe5dbee17328a31c3d to your computer and use it in GitHub Desktop.
Human Readable File Size with PHP
<?php
# http://jeffreysambells.com/2012/10/25/human-readable-filesize-php
function human_filesize($bytes, $decimals = 2) {
$size = array('B','kB','MB','GB','TB','PB','EB','ZB','YB');
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor];
}
echo human_filesize(filesize('example.zip'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment