PHP filesize MB/KB conversion

How can I convert the output of PHP s filesize() function to a nice format with MegaBytes, KiloBytes etc?

you can use this fuction in your php website it will convert file size it is very useful function i am also using it.


<?php

    function formatSizeUnits($bytes)

    {

        if ($bytes >= 1073741824)

        {

            $bytes = number_format($bytes / 1073741824, 2) . 'GB';

        }

        elseif ($bytes >= 1048576)

        {

            $bytes = number_format($bytes / 1048576, 2) . 'MB';

        }

        elseif ($bytes >= 1024)

        {

            $bytes = number_format($bytes / 1024, 2) . 'KB';

        }

        elseif ($bytes > 1)

        {

            $bytes = $bytes . 'bytes';

        }

        elseif ($bytes == 1)

        {

            $bytes = $bytes . 'byte';

        }

        else

        {

            $bytes = '0 bytes';

        }


        return $bytes;

}


?>

PHP Tricks 2016-07-15View: 1226

Categories: PHP Tricks

Comments

Leave a comment