Re: filesize math

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, 24 Dec 2004 04:15:10 -0500, Sebastian
<sebastian@xxxxxxxxxxxxxxxxxxx> wrote:
> i made this function and want to know if i am doing the math correctly..
> seems to be caculating ok.. $filesize is in bytes.. if the filesize is under
> 1MB i want to show KBs, if its under 1GB i want to show MB, if its over
> 1000MB i want to show GB, makes sense? ;)
> 
> function byte_format($filesize)
> {
>     if ($filesize < 1000000)
>     {
>          return number_format($filesize / 1024, 2, '.', '') . ' KB';
>     }
>     else if($filesize > 1000000000)
>     {
>         return number_format($filesize / 1024 / 1024 / 1024, 2, '.', '') .
> ' GB';
>     }
>     else
>     {
>        return number_format($filesize / 1024 / 1024, 2, '.', '') . ' MB';
>     }
> }

Seems overly complex.  Why not something like:

$file_name = "/path/to/file";
$file_type = array( 'K', 'M', 'G' );
$size = filesize ( $file_name );
for ( $t = 0; $size > 1024; $t++ )
{
    $size /= 1024;
    $file_size = round ( $size, 1 ) . ' ' . $file_type[ $t ] . 'B';
}
echo "\$file_size = $file_size";


-- 
Greg Donald
Zend Certified Engineer
http://gdconsultants.com/
http://destiney.com/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux