Return to Snippet

Revision: 19304
at October 21, 2009 04:58 by stancox


Initial Code
<?php

function ByteSize($bytes) 
    {
    $size = $bytes / 1024;
    if($size < 1024)
        {
        $size = number_format($size, 2);
        $size .= ' KB';
        } 
    else 
        {
        if($size / 1024 < 1024) 
            {
            $size = number_format($size / 1024, 2);
            $size .= ' MB';
            } 
        else if ($size / 1024 / 1024 < 1024)  
            {
            $size = number_format($size / 1024 / 1024, 2);
            $size .= ' GB';
            } 
        }
    return $size;
    }

// Returns '19.28mb'
print ByteSize('20211982');

?>

Initial URL

                                

Initial Description
Takes a number in bytes and returns the number in (KB,MB,GB) for human-readable format.

Initial Title
Convert Bytes to corresponding size

Initial Tags
convert

Initial Language
PHP