Laravel Utilities

Format Raw Bytes into Human-Readable File Sizes with Number::fileSize()

Punyapal Shah 1 min read
edit this tip
Use Number::fileSize() to convert raw byte integers into formatted KB, MB, GB, and TB strings with customizable precision.

When displaying file upload sizes or storage quotas (such as 10485760 bytes), calculating power-of-1024 divisions manually with log math is tedious.

The Number::fileSize() helper converts raw byte counts into localized, readable file size strings.

Basic Usage

use Illuminate\Support\Number;

// Automatically resolves to MB
echo Number::fileSize(1024 * 1024 * 5);
// Output: "5 MB"

// Formats fractional bytes with custom precision
echo Number::fileSize(1572864, precision: 2);
// Output: "1.50 MB"

// Large file storage
echo Number::fileSize(1099511627776);
// Output: "1 TB"

In Blade Templates

<div class="attachment-item">
    <span>{{ $document->name }}</span>
    <span class="text-muted">{{ Number::fileSize($document->size_in_bytes) }}</span>
</div>

Summary

  • Converts byte integers into B, KB, MB, GB, TB, and PB.
  • Supports precision overrides via the precision argument.
  • Cleanly replaces custom byte formatting helper functions.
Tags: Laravel Formatting Files Utilities
Share on X

// Found an issue or want to contribute a tip? github.com/MrPunyapal/tips