Plank\Mediable\Helpers\File::readableSize PHP Method

readableSize() public static method

Generate a human readable bytecount string.
public static readableSize ( integer $bytes, integer $precision = 1 ) : string
$bytes integer
$precision integer
return string
    public static function readableSize($bytes, $precision = 1)
    {
        static $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
        if ($bytes === 0) {
            return '0 ' . $units[0];
        }
        $exponent = floor(log($bytes, 1024));
        $value = $bytes / pow(1024, $exponent);
        return round($value, $precision) . ' ' . $units[$exponent];
    }

Usage Example

Example #1
0
 public function test_it_converts_bytes_to_readable_strings()
 {
     $this->assertEquals('0 B', File::readableSize(0));
     $this->assertEquals('1 KB', File::readableSize(1025, 0));
     $this->assertEquals('1.1 MB', File::readableSize(1024 * 1024 + 1024 * 100, 2));
 }
All Usage Examples Of Plank\Mediable\Helpers\File::readableSize