Human readable filesize

Submitted by:Jhon Jhon

Date added:27 May, 2015

Category:PHP

A very common task is converting a number of bytes into something for human digestion.

Tags: readable filesize , count filesize

Code Snippet:

// 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
*/
function HumanReadableFilesize($size) {

$mod = 1024;

$units = explode(' ','B KB MB GB TB PB');
for ($i = 0; $size > $mod; $i++) {
$size /= $mod;
}

return round($size, 2) . ' ' . $units[$i];
}
 
 

Comments