human readable file size

前端 未结 11 2217
梦毁少年i
梦毁少年i 2020-12-25 10:48
function humanFileSize($size)
{
    if ($size >= 1073741824) {
      $fileSize = round($size / 1024 / 1024 / 1024,1) . \'GB\';
    } elseif ($size >= 1048576)          


        
相关标签:
11条回答
  • 2020-12-25 11:38
    function getHumanReadableSize($size, $unit = null, $decemals = 2) {
        $byteUnits = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
        if (!is_null($unit) && !in_array($unit, $byteUnits)) {
            $unit = null;
        }
        $extent = 1;
        foreach ($byteUnits as $rank) {
            if ((is_null($unit) && ($size < $extent <<= 10)) || ($rank == $unit)) {
                break;
            }
        }
        return number_format($size / ($extent >> 10), $decemals) . $rank;
    }
    

    If php version below 5.4 use $byteUnits = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');

    0 讨论(0)
  • 2020-12-25 11:47

    Here is a working function managing till Yottabyte:

    function DisplayFileSize($size, $unit = false, $precision = 2){
    
    $b = $size;
    $kb = round($size / 1024, $precision);
    $mb = round($kb / 1024, $precision);
    $gb = round($mb / 1024, $precision);
    $tb = round($gb / 1024, $precision);
    $pb = round($tb / 1024, $precision);
    $eb = round($pb / 1024, $precision);
    $zb = round($eb / 1024, $precision);
    $yb = round($zb / 1024, $precision);
    
    if((!$unit && floor($kb) == 0) || $unit == "b") {
        return array("value" => FormatNumber($b), "unit" => "bytes");
    } else if((!$unit && floor($mb) == 0) || $unit == "kb") {
        return array("value" => FormatNumber($kb, 2), "unit" => "Kb");
    } else if((!$unit && floor($gb) == 0) || $unit == "mb") {
        return array("value" => FormatNumber($mb, 2), "unit" => "Mb");
     } else if((!$unit && floor($tb) == 0) || $unit == "gb") {
         return array("value" => FormatNumber($gb, 2), "unit" => "Gb");
     } else if((!$unit && floor($pb) == 0) || $unit == "tb") {
         return array("value" => FormatNumber($tb, 2), "unit" => "Tb");
     } else if((!$unit && floor($eb) == 0) || $unit == "pb") {
        return array("value" => FormatNumber($pb, 2), "unit" => "Pb");
     } else if((!$unit && floor($zb) == 0) || $unit == "eb") {
         return array("value" => FormatNumber($eb, 2), "unit" => "Eb");
     } else if((!$unit && floor($yb) == 0) || $unit == "zb") {
         return array("value" => FormatNumber($zb, 2), "unit" => "Zb");
    } else {
        return array("value" => FormatNumber($yb, 2), "unit" => "Yb");
    }
    

    }

    0 讨论(0)
  • 2020-12-25 11:50

    There is great example by Jeffrey Sambells:

    function human_filesize($bytes, $dec = 2) 
    {
        $size   = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
        $factor = floor((strlen($bytes) - 1) / 3);
    
        return sprintf("%.{$dec}f", $bytes / pow(1024, $factor)) . @$size[$factor];
    }
    
    print human_filesize(filesize('example.zip'));
    
    0 讨论(0)
  • 2020-12-25 11:52

    You can modify your function to fullfil both your need to force a unit if given and adjust the precision.

    function humanFileSize($size, $precision = 1, $show = "")
    {
        $b = $size;
        $kb = round($size / 1024, $precision);
        $mb = round($kb / 1024, $precision);
        $gb = round($mb / 1024, $precision);
    
        if($kb == 0 || $show == "B") {
            return $b . " bytes";
        } else if($mb == 0 || $show == "KB") {
            return $kb . "KB";
        } else if($gb == 0 || $show == "MB") {
            return $mb . "MB";
        } else {
            return $gb . "GB";
        }
    }
    
    //Test with different values
    echo humanFileSize(1038) . "<br />";    
    echo humanFileSize(103053, 0) . "<br />";
    echo humanFileSize(103053) . "<br />";
    echo humanFileSize(1030544553) . "<br />";
    echo humanFileSize(1030534053405, 2, "GB") . "<br />";  ;
    
    0 讨论(0)
  • 2020-12-25 11:53

    A pretty short 3 lines method that I use (1024 = 1KB) and supports from KB to YB is the following one:

    <?php 
    
    /**
     * Converts a long string of bytes into a readable format e.g KB, MB, GB, TB, YB
     * 
     * @param {Int} num The number of bytes.
     */
    function readableBytes($bytes) {
        $i = floor(log($bytes) / log(1024));
    
        $sizes = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
    
        return sprintf('%.02F', $bytes / pow(1024, $i)) * 1 . ' ' . $sizes[$i];
    }
    
    // "1000 B"
    echo readableBytes(1000);
    
    // "9.42 MB"
    echo readableBytes(9874321);
    
    // "9.31 GB"
    // The number of bytes as a string is accepted as well
    echo readableBytes("10000000000");
    
    // "648.37 TB"
    echo readableBytes(712893712304234);
    
    // "5.52 PB"
    echo readableBytes(6212893712323224);
    

    More info about these methods on this article.

    0 讨论(0)
提交回复
热议问题