Sponsored by NuSphere - PHP Software for PHP Application Developers - On Sale This Week for $100 off


PHP Tutorials and Scripts   




Title: Bytes Scale function    Marked Cool    (Review this resource)
Author: silence
Posted On: 2004-11-29
Category: Home > PHP Functions

Popularity: 1 points out of 10    

Description: Returns the nearest prefix (k, M, G) for $bytes (given parameter).

Total Hits: 909     Total Votes: 0     Total Points: 0 (0 reviews)        [ Download ]       [ Print ]   

Page Navigation:  [1]


Bytes Scale function





This function calculates the nearest prefix for bytes in kilobits (KB), megabits (MB) or gigabits (GB).

Code is:


<?


//
// Returns 10 raised to the power of $exp.
//
function pow10($exp)
{
return 
pow(10$exp);
}

//
// Returns the nearest prefix for $bytes
//
function bytes_scale($bytes)
{
    if (
$bytes pow10(3) )
        return 
$bytes.' B';

    elseif (
$bytes >= pow10(3) && $bytes pow10(6) )
        return 
round($bytes/pow10(3), 2).' kB';

    elseif (
$bytes >= pow10(6) && $bytes pow10(9) )
        return 
round($bytes/pow10(6), 2).' MB';

    else
        return 
round($bytes/pow10(9), 2).' GB';
}

?>

by Bartek Nowotarski



Page Navigation:  [1]



© Copyright 2003-2008 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.