PHPackages                             elephant-php/byte-size - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. elephant-php/byte-size

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

elephant-php/byte-size
======================

ByteSize uses binary conversion (1024-based) for file-size formatting.

0.2.0(4w ago)00MITPHPPHP ^8.1

Since Apr 27Pushed 4w agoCompare

[ Source](https://github.com/elephant-php/byte-size)[ Packagist](https://packagist.org/packages/elephant-php/byte-size)[ RSS](/packages/elephant-php-byte-size/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (2)Dependencies (1)Versions (3)Used By (0)

 [ ![elephant-php](elephant-php.webp) ](https://github.com/elephant-php/byte-size)

ByteSize
========

[](#bytesize)

A small value object for working with file sizes in a clean and predictable way.

 [![Latest Version](https://camo.githubusercontent.com/b8b7202de2098cc97c14adc8fe90380f93cfe4cf0261a63060497b2d40bf5ea5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f656c657068616e742d7068702f627974652d73697a652e737667)](https://packagist.org/packages/elephant-php/byte-size) [![License](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](./LICENSE.md) [![PHP 8.1+](https://camo.githubusercontent.com/63f4496656b9cc2ffb21b45d7017e6aee05d1c13f1b41d1e1c8b4d939c5e2708/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e312532422d3737376262342e737667)](https://www.php.net/releases/8.1/en.php)

It uses `1024` based conversion and gives you a simple API to:

- create sizes from bytes, KB, MB, GB, or TB
- convert values between units
- format values for UI or CLI output
- generate human-readable file size strings

Installation
------------

[](#installation)

```
composer require elephant-php/byte-size
```

Requirements
------------

[](#requirements)

- PHP `8.1` or higher

Quick example
-------------

[](#quick-example)

```
use ElephantPhp\ByteSize\ByteSize;
use ElephantPhp\ByteSize\ByteSizeUnit;

$size = ByteSize::fromBytes(2_621_440);

echo $size->toMegabytes();                      // 2.5
echo $size->format(ByteSizeUnit::MEGABYTES);    // 2.5 MB
echo $size->human();                            // 2.5 MB
echo $size->human(0);                           // 2 MB
echo $size->human(0, 'Megabytes');              // 2 Megabytes
echo (string) $size;                            // 2.5 MB
```

Real-world example
------------------

[](#real-world-example)

```
use ElephantPhp\ByteSize\ByteSize;

$bytes = filesize('/path/to/archive.zip');
$size = ByteSize::fromBytes($bytes);

echo $size->human(); // e.g. "14.8 MB"
```

Creation
--------

[](#creation)

```
use ElephantPhp\ByteSize\ByteSize;

ByteSize::fromBytes(2621440);
ByteSize::fromKilobytes(2560);
ByteSize::fromMegabytes(2.5);
ByteSize::fromGigabytes(0.0025);
ByteSize::fromTerabytes(0.0000025);
```

Short aliases are available too:

```
ByteSize::bytes(2621440);
ByteSize::kb(2560);
ByteSize::mb(2.5);
ByteSize::gb(0.0025);
ByteSize::tb(0.0000025);
```

Conversion
----------

[](#conversion)

```
use ElephantPhp\ByteSize\ByteSize;

$size = ByteSize::fromBytes(2_621_440);

$size->toBytes();      // 2621440
$size->toKilobytes();  // 2560
$size->toMegabytes();  // 2.5
$size->toGigabytes();  // 0.00244140625
$size->toTerabytes();  // 0.000002384185791015625
```

Short aliases:

```
$size->toKb();
$size->toMb();
$size->toGb();
$size->toTb();
```

Formatting
----------

[](#formatting)

Use `format()` when you want an explicit unit.

```
use ElephantPhp\ByteSize\ByteSize;
use ElephantPhp\ByteSize\ByteSizeUnit;

$size = ByteSize::fromBytes(2_621_440);

echo $size->format(ByteSizeUnit::TERABYTES, 7);       // 0.0000024 TB
echo $size->format(ByteSizeUnit::TERABYTES, 7, '');   // 0.0000024
echo $size->format(ByteSizeUnit::MEGABYTES, 2);       // 2.5 MB
echo $size->format(ByteSizeUnit::MEGABYTES, 2, 'МБ'); // 2.5 МБ
```

- `$precision` controls the number of decimal places before trailing zeros are trimmed
- `$label` overrides the default unit label
- passing `''` as label returns only the numeric value

Human-readable output
---------------------

[](#human-readable-output)

Use `human()` when you want `ByteSize` to choose the most suitable unit automatically.

```
use ElephantPhp\ByteSize\ByteSize;

$size = ByteSize::fromBytes(2_621_440);

echo $size->human();              // 2.5 MB
echo $size->human(2, 'MEGABYTE'); // 2.5 MEGABYTE
echo (string) $size;              // 2.5 MB
```

Examples:

```
ByteSize::fromBytes(999)->human();                 // 999 B
ByteSize::fromBytes(1024)->human();                // 1 KB
ByteSize::fromBytes(1048576)->human();             // 1 MB
ByteSize::fromBytes(1073741824)->human();          // 1 GB
ByteSize::fromBytes(1099511627776)->human();       // 1 TB
```

Available units
---------------

[](#available-units)

```
use ElephantPhp\ByteSize\ByteSizeUnit;

ByteSizeUnit::BYTES;
ByteSizeUnit::KILOBYTES;
ByteSizeUnit::MEGABYTES;
ByteSizeUnit::GIGABYTES;
ByteSizeUnit::TERABYTES;
```

License
-------

[](#license)

MIT License

Please see [`LICENSE`](./LICENSE.md) for more information.

###  Health Score

34

—

LowBetter than 75% of packages

Maintenance94

Actively maintained with recent releases

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity33

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~14 days

Total

2

Last Release

29d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/98bd41d83d3ea8c774bfffa19be2323a844609643a8c4164bbb2ac9f74615923?d=identicon)[Pekhov Anton](/maintainers/Pekhov%20Anton)

---

Top Contributors

[![Pekhov14](https://avatars.githubusercontent.com/u/24500560?v=4)](https://github.com/Pekhov14 "Pekhov14 (10 commits)")

---

Tags

byte-sizebytesconversionfile-sizefilesizeformatterphpvalue-objectformatterValue Objectconversionbytesfilesizefile-sizebyte-size

### Embed Badge

![Health badge](/badges/elephant-php-byte-size/health.svg)

```
[![Health](https://phpackages.com/badges/elephant-php-byte-size/health.svg)](https://phpackages.com/packages/elephant-php-byte-size)
```

###  Alternatives

[moneyphp/money

PHP implementation of Fowler's Money pattern

4.8k88.3M492](/packages/moneyphp-money)[cuyz/valinor

Dependency free PHP library that helps to map any input into a strongly-typed structure.

1.5k11.7M152](/packages/cuyz-valinor)[florianv/swap

PHP currency conversion library for retrieving exchange rates from 30 providers, with caching and fallback.

1.3k6.7M22](/packages/florianv-swap)[spatie/color

A little library to handle color conversions

38120.5M33](/packages/spatie-color)[php-units-of-measure/php-units-of-measure

A PHP library for converting between standard units of measure.

3143.7M30](/packages/php-units-of-measure-php-units-of-measure)[ozdemirburak/iris

PHP library for color manipulation and conversion.

1201.8M20](/packages/ozdemirburak-iris)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
