Aidan's PHP Repository

A repository for PHP functions and classes ...

function.size_readable.php

Return human readable sizes

  • Author: Aidan Lister <aidan@php.net>
  • Version: 1.1.0
  • Link: http://aidanlister.com/repos/v/function.size_readable.php
  • Views: 24032
  • Downloads: 2226

Source

Download this script <?php
/**
 * Return human readable sizes
 *
 * @author      Aidan Lister <aidan@php.net>
 * @version     1.1.0
 * @link        http://aidanlister.com/repos/v/function.size_readable.php
 * @param       int    $size        Size
 * @param       int    $unit        The maximum unit
 * @param       int    $retstring   The return string format
 * @param       int    $si          Whether to use SI prefixes
 */
function size_readable($size, $unit = null, $retstring = null, $si = true)
{
    // Units
    if ($si === true) {
        $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB');
        $mod   = 1000;
    } else {
        $sizes = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB');
        $mod   = 1024;
    }
    $ii = count($sizes) - 1;
 
    // Max unit
    $unit = array_search((string) $unit, $sizes);
    if ($unit === null || $unit === false) {
        $unit = $ii;
    }
 
    // Return string
    if ($retstring === null) {
        $retstring = '%01.2f %s';
    }
 
    // Loop
    $i = 0;
    while ($unit != $i && $size >= 1024 && $i < $ii) {
        $size /= $mod;
        $i++;
    }
 
    return sprintf($retstring, $size, $sizes[$i]);
}
 
?>

Example

<pre>
<?php
require_once 'function.size_readable.php';
 
// Simple
echo "Simple:\n";
echo size_readable(55), "\n";
echo size_readable(55555555555555), "\n\n";
 
// Maximum unit
echo "Max units in MB:\n";
echo size_readable(55555555555555, 'MB'), "\n\n";
 
// 4 decimal accuracy
echo "4 decimals:\n";
echo size_readable(55555555555555, null, '%01.4f %s'), "\n\n";
 
// 1 decimal accuracy, units in brackets, max unit in MB
echo "1 decimal, units in brackets, max unit of MB:\n";
$size = disk_total_space('/home/aidan');
echo size_readable($size, 'MB', '%01.1f (%s)');
?>
</pre>

Output

Simple:
55.00 B
55.56 TB

Max units in MB:
55555555.56 MB

4 decimals:
55.5556 TB

1 decimal, units in brackets, max unit of MB:
231769.3 (MB)

Comments

March 5th, 2006
Nice script...
December 1st, 2005
Thank you! Your code was a GOD-SEND!
October 6th, 2005
Awesome code, appreciate it, and the site looks great as well! :D
August 4th, 2005
Hi, nice code. Any way this can be applied to network/mapped drives? [Editor's Note: It'll work on anything, it takes numerical input. How you get that number is up to you :)]
March 21st, 2005
Hahaha :D nice script btw..
February 26th, 2005
You should add: PetaByte ( 1024 TB ) ExaByte ( 1024 PB ) ZettaByte ( 1024 EB ) YottaByte ( 1024 ZB ) [Editor's Note: I've added PetaByte, but anything larger than that will not come into _practical_ existance in my lifetime, and I dare say PHPs, so I see no point in adding them.]
December 7th, 2004
I didn't know you could use "$foo = null" as a passed in parameter, that is super-useful for me. I missed Bash's allowance for mixed number of parameters, and while this isn't perfect, it is a hell of a lot better than forcing all parameters to be entered. Thanks.
November 5th, 2004
Hi Aidan, That is a neat little script, but have you thought about forcing the units to say kilobytes and adding commas? Keith [Editor's Note: The function now uses the sprintf modifier, so you can format the output string however you wish.]
November 5th, 2004
Hi Aidan, The function is great but it does not work properly in PHP < 4.2.0 because of the function array_search() returns NULL on failure instead of FALSE. I think it would be better to do the double check (NULL or FALSE). Greetings Armando Anton [Editor's Note: Useful tip, I've updated this in the code - thanks very much]
November 5th, 2004
Hi Aidan, I see you default $retstring to null, then later check to see if it is null, and if so set it to '%01.2f %s'. Why not default to '%01.2f %s'? Thanks for the code, AJ [Editor's Note: I try to wrap all my code before 65 characters as per the PEAR coding standard. Not a great excuse, but it looks nicer :)]