Oihana PHP

formatFileSize.php

Table of Contents

Functions

formatFileSize()  : string
Formats a byte count as a human-readable string.

Functions

formatFileSize()

Formats a byte count as a human-readable string.

formatFileSize(int $bytes[, int $precision = 2 ]) : string

Uses binary multiples (base 1024) with the FileSizeUnit symbols B, KB, MB, GB, TB, PB. Byte values are rendered without decimals; larger units use $precision decimals. Zero or negative values yield "0 B". Values beyond PB are clamped to PB.

Parameters
$bytes : int

The size in bytes (e.g. from getFileSize()).

$precision : int = 2

Number of decimals for non-byte units (default: 2).

Tags
author

Marc Alcaraz (ekameleon)

since
1.2.0
example
use function oihana\files\formatFileSize;

formatFileSize( 0 ) ;        // "0 B"
formatFileSize( 512 ) ;      // "512 B"
formatFileSize( 1536 ) ;     // "1.5 KB"
formatFileSize( 1240518 ) ;  // "1.18 MB"
Return values
string

A human-readable size, e.g. "1.18 MB".

On this page

Search results