PHPackages                             nabeghe/file-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. nabeghe/file-size

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

nabeghe/file-size
=================

v1.0.0(10mo ago)15MITPHPPHP &gt;=7.4

Since Jul 19Pushed 10mo agoCompare

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

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

FileSize Helper for PHP ≥ 7.4
=============================

[](#filesize-helper-for-php--74)

> A simple, handy PHP library to work with file sizes and units.

Convert, compare, parse, and format sizes in bytes, bits, kilobytes, megabytes... you name it.
Supports bits **and** bytes, plus all common units up to yottabytes.

---

Features
--------

[](#features)

- Convert sizes between any units (e.g. MB → GB, bits → bytes)
- Generate human-readable size strings from raw values or unit-based input
- Parse human-readable strings like `"1.5 GB"` or `"200 Kb"`
- Auto-detect the best unit for a given raw size (e.g. 1536000 → MB)
- Format numbers cleanly with optional decimal precision
- Compare two sizes across different units
- Get percentage relations between sizes
- Validate units easily (`B`, `KB`, `Mb`, `Gb`, etc.)
- Customize unit labels for flexible display

### Supported Units

[](#supported-units)

- Bytes: `B, KB, MB, GB, TB, PB, EB, ZB, YB`
- Bits: `b, Kb, Mb, Gb, Tb, Pb, Eb, Zb, Yb`

---

🫡 Usage
-------

[](#-usage)

### 🚀 Installation

[](#-installation)

You can install the package via composer:

```
composer require nabeghe/filesize
```

Or manually include the FileSize.php if you want to keep it old school.

### Examples

[](#examples)

**First:**

```
use Nabeghe\FileSize;
```

### Get a list of all supported units

[](#get-a-list-of-all-supported-units)

**Syntax:** `getAllUnits(bool $bits = false): array`

```
FileSize::getAllUnits();     // ['B', 'KB', 'MB', 'GB', ...]
FileSize::getAllUnits(true); // ['b', 'Kb', 'Mb', 'Gb', ...]
```

### Check if a unit is supported

[](#check-if-a-unit-is-supported)

**Syntax:** `isValidUnit(string $unit): bool`

```
FileSize::isValidUnit('MB');  // true
FileSize::isValidUnit('xyz'); // false
```

### Clean up a number to a fixed decimal format

[](#clean-up-a-number-to-a-fixed-decimal-format)

**Syntax:** `format(float|int $size, int $decimals = 2): int|float`

```
FileSize::format(13);         // 13
FileSize::format(14);         // 14
FileSize::format(100.456);    // 100.46
FileSize::format(100.000001); // 100
FileSize::format(313.1314, 3) // 313.131
```

### Convert between units

[](#convert-between-units)

**Syntax:** `convert($size, $fromUnit, $toUnit, $format = false, $isBinary = true)`

```
FileSize::convert(1, 'GB', 'MB');               // 1024
FileSize::convert(1024, 'KB', 'MB');            // 1
FileSize::convert(1, 'GB', 'Mb');               // 8192
FileSize::convert(1234, 'MB', 'GB', 1);         // 1.2
FileSize::convert(1, 'Gb', 'Mb');               // 1024
FileSize::convert(1, 'Gb', 'Mb', false, false); // 1000 (decimal)
```

### Compare two sizes, even if in different units

[](#compare-two-sizes-even-if-in-different-units)

**Syntax:** `compare($size1, $unit1, $size2, $unit2): int`

```
FileSize::compare(1, 'GB', 1024, 'MB'); // 0 (equal)
FileSize::compare(1, 'GB', 2, 'GB');    // -1 (smaller)
FileSize::compare(3, 'Mb', 2, 'MB');    // -1 (smaller, because 3Mb ≈ 0.375MB)
```

### Find the best-fit unit for a byte size

[](#find-the-best-fit-unit-for-a-byte-size)

**Syntax:** `detectUnit($size, $isBits = false, &$finalSize = null, bool $isBinary = true: string`

```
$unit = FileSize::detectUnit(123456789, false, $finalSize); // binary=1024
// $unit => 'MB', $finalSize => 117.74

$unit = FileSize::detectUnit(123456789, false, $finalSize, false); // decimal=1000
// $unit => 'MB', $finalSize => 123.46
```

### Turn a byte or bit size into a human-readable string

[](#turn-a-byte-or-bit-size-into-a-human-readable-string)

**Syntax:** `readable($size, $isBits = false, $labels = [])`

```
FileSize::readable(123456789);                                  // "117.74 MB"
FileSize::readable(123456789, false, null, false);              // "123.46 MB"
FileSize::readable(123456789, true);                            // "117.74 Mb"
FileSize::readable(123456789, true, null, false);               // "123.46 Mb"
FileSize::readable(1024**3, false, ['GB' => 'Gigabyte'])        // "1 Gigabyte"
FileSize::readable(1024**3, false, ['GB' => 'Gigabyte'], false) // "1.07 Gigabyte"
```

### Convert a size from a specific unit and make it readable

[](#convert-a-size-from-a-specific-unit-and-make-it-readable)

**Syntax:** `readableFromUnit($size, $unit, $labels = [], bool $isBinary = true)`

```
FileSize::readableFromUnit(2048, 'KB');              // "2 MB"
FileSize::readableFromUnit(2000, 'KB', null, false); // "2 MB"
FileSize::readableFromUnit(2048, 'Kb');              // "2 Mb"
FileSize::readableFromUnit(1, 'Gb');                 // "1 Gb"
```

### Extract numeric value and unit from a string

[](#extract-numeric-value-and-unit-from-a-string)

**Syntax:** `parse(string $input): array`

```
FileSize::parse('1.5 GB');   // [1.5, 'GB']
FileSize::parse('  200 Kb'); // [200, 'Kb']
FileSize::parse('3mb');      // [3.0, 'mb']
```

### Calculate how much size1 is of size2 in %

[](#calculate-how-much-size1-is-of-size2-in-)

**Syntax:** `percentage($size1, $unit1, $size2, $unit2, bool $isBinary = true): float`

```
FileSize::percentage(512, 'MB', 1, 'GB'); // 50.0
FileSize::percentage(100, 'Kb', 1, 'Mb'); // 10.0
```

📖 License
---------

[](#-license)

Licensed under the MIT license, see [LICENSE.md](LICENSE.md) for details.

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance55

Moderate activity, may be stable

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity36

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

Unknown

Total

1

Last Release

303d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/dae1bf378bcf40fe55c193160377d613d628188587554a3fb11cdec6f0521ba1?d=identicon)[nabeghe](/maintainers/nabeghe)

---

Top Contributors

[![nabeghe](https://avatars.githubusercontent.com/u/12207627?v=4)](https://github.com/nabeghe "nabeghe (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/nabeghe-file-size/health.svg)

```
[![Health](https://phpackages.com/badges/nabeghe-file-size/health.svg)](https://phpackages.com/packages/nabeghe-file-size)
```

PHPackages © 2026

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