PHPFront

Home > PHP > Convert Bytes to corresponding size
The PHP website created by its users!



Convert Bytes to corresponding size


Authormkeefe
DescriptionTakes a number in bytes and returns the number in (KB,MB,GB) for human-readable format.
RatingThis script has been rated 4/5 (21 votes)

PHP Code


<?php

function ByteSize($bytes
    {
    
$size $bytes 1024;
    if(
$size 1024)
        {
        
$size number_format($size2);
        
$size .= ' KB';
        } 
    else 
        {
        if(
$size 1024 1024
            {
            
$size number_format($size 10242);
            
$size .= ' MB';
            } 
        else if (
$size 1024 1024 1024)  
            {
            
$size number_format($size 1024 10242);
            
$size .= ' GB';
            } 
        }
    return 
$size;
    }

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

?>



Rate it: Print Print

Comments


Dhanesh / 3 Nov 05

Hi,
Good code.But I got an error in this line
else($size / 1024 / 1024 < 1024)

It will work if we add like
else if ($size / 1024 / 1024 < 1024)
Annonymous / 18 Nov 05

oh my, your right. When I reformatted it I must have taken off the "elseif" My deepest apoligies for that.

Matt
Matthew / 18 Nov 05

The below comment was mine 'mkeefe'. I was under the impression that this site would keep your name on file?
Admin / 3 Jan 06

- Code Edited -

convert in TB ??

hohohoho!!!
Matthew / 31 Mar 06

You can add TB support. :)
Name
Website
Your comment