size_readable ( $size, $unit, $default )

Return human readable file sizes.

Access

public

Parameters

Parameter

Type

Required

Description

$size

integer

Required

Filesize in bytes.

$unit

string

Optional

The maximum unit to use. Defaults to the largest appropriate unit.

$default

string

Optional

The format for the return string. Defaults to %01.2f %s.

Returns

Type

Description

string

The human-readable file size.

Examples

Convert a number of bytes into a human-readable format.

// Instantiate
$s3 = new AmazonS3();

// Test data
var_dump($s3->util->size_readable(9876543210));
Result:
string(7) "9.20 GB"

See Also

Source

Method defined in utilities/utilities.class.php | Toggle source view (30 lines) | View on GitHub

public function size_readable($size, $unit = null, $default = null)
{
    // Units
    $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB');
    $mod = 1024;
    $ii = count($sizes) - 1;

    // Max unit
    $unit = array_search((string) $unit, $sizes);
    if ($unit === null || $unit === false)
    {
        $unit = $ii;
    }

    // Return string
    if ($default === null)
    {
        $default = '%01.2f %s';
    }

    // Loop
    $i = 0;
    while ($unit != $i && $size >= 1024 && $i < $ii)
    {
        $size /= $mod;
        $i++;
    }

    return sprintf($default, $size, $sizes[$i]);
}

Copyright © 2010–2013 Amazon Web Services, LLC


Feedback