Human readable filesize

Returns a human readable filesize.

// A much better and accurate version can be found
// in Aidan's PHP Repository: 
// http://aidanlister.com/repos/v/function.size_readable.php
 
/**
 * Returns a human readable filesize
 *
 * @author      wesman20 (php.net)
 * @author      Jonas John
 * @version     0.3
 * @link        https://www.jonasjohn.de/snippets/php/readable-filesize.htm
 */
function HumanReadableFilesize($size) {
 
    // Adapted from: http://www.php.net/manual/en/function.filesize.php
 
    $mod = 1024;
 
    $units = explode(' ','B KB MB GB TB PB');
    for ($i = 0; $size > $mod; $i++) {
        $size /= $mod;
    }
 
    return round($size, 2) . ' ' . $units[$i];
}
Snippet Details



// Example
 
print HumanReadableFilesize(filesize('test_2mb.zip'));
 
// Returns: 
// --> 2 MB

Sorry folks, comments have been deactivated for now due to the large amount of spam.

Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!


Older comments:

NeoAcheron July 04, 2011 at 15:33
Excellent! Thank you very much!
Horacio June 04, 2011 at 00:56
Excelent.
anony May 31, 2011 at 22:09
wonderfull!
Surger March 01, 2011 at 10:00
Thank you!