Support the ongoing development of Laravel.io →
Article Hero Image

Human-Readable File Sizes in Laravel (KB, MB, GB)

31 Mar, 2025 3 min read

Photo by Wesley Tingey on Unsplash

Introduction

There may be times when you want to display file sizes to your users in a human-readable format.

For example, you may have an area in your application which allows users to download files. Rather than displaying a downloadable file's file size in bytes, you may want to display the file size in KB, MB, GB, etc. Let's be honest, it's much easier to understand a file size of "2.5 MB" than "2,500,000 bytes", especially for non-technical users.

In this Quickfire article, we'll take a look at how to display file sizes in a human-readable format in your Laravel applications.

Pre-requisites

Before we get started, it's worth noting that the examples covered in this article require the intl PHP extension to be installed.

Human-Readable File Sizes in Laravel

To display file sizes in a human-readable format, we can use the Illuminate\Support\Number class in Laravel.

Note: The Illuminate\Support\Number class has a bunch of super handy methods. If you're interested, I have another article (Formatting and Spelling Ordinal Numbers in PHP and Laravel) which covers this class' ordinal and spellOrdinal methods.

We can call the fileSize method and it will convert the number of bytes to a human-readable format:

use Illuminate\Support\Number;

Number::fileSize(bytes: 1024); // 1 KB
Number::fileSize(bytes: 2135); // 2 KB
Number::fileSize(bytes: 2_000_000); // 2 MB
Number::fileSize(bytes: 3_048_000); // 3 MB
Number::fileSize(bytes: 4_000_000_000); // 4 GB

By default, the fileSize method will round the file size to a whole number. In some cases, this may be suitable for you, but in other cases, you may want to display the file size with a fixed number of decimal places.

To do this, we can pass a precision argument to the fileSize method:

use Illuminate\Support\Number;

Number::fileSize(bytes: 1024, precision: 2); // 1.00 KB
Number::fileSize(bytes: 2135, precision: 2); // 2.08 KB
Number::fileSize(bytes: 2_000_000, precision: 2); // 1.91 MB
Number::fileSize(bytes: 3_048_000, precision: 2); // 2.91 MB
Number::fileSize(bytes: 4_000_000_000, precision: 2); // 3.73 GB

In our example, we've used the precision argument to specify that we always want to display the file size with 2 decimal places.

Although this can give us a more accurate representation of the file size, it can sometimes add unneeded trailing zeros. For example, rather than displaying "1 KB", it will display "1.00 KB".

To remove the decimal places from whole numbers, we can pass a maxPrecision argument to the method like so:

use Illuminate\Support\Number;

Number::fileSize(bytes: 1024, maxPrecision: 2); // 1 KB
Number::fileSize(bytes: 2135, maxPrecision: 2); // 2.08 KB
Number::fileSize(bytes: 2_000_000, maxPrecision: 2); // 1.91 MB
Number::fileSize(bytes: 3_048_000, maxPrecision: 2); // 2.91 MB
Number::fileSize(bytes: 4_000_000_000, maxPrecision: 2); // 3.73 GB

In the example above, we've specified that we want to display the file size with a maximum of 2 decimal places. As a result, if the file size is a whole number, it will be displayed without any decimal places.)

Example Use Case

To help you understand how you might use the fileSize method in your application, here's an example of how you could use it to get the human-friendly file size of a file.

We'll assume we have an example-image.png image which is stored in our local storage/app/private directory and that we're using the local filesystem disk.

If we wanted to get the human-readable file size for this image, we could do the following:

use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Number;

$humanFriendlySize = Number::fileSize(
    bytes: Storage::fileSize('example-image.png'),
);

Let's assume the file size is 51,620 bytes. The $humanFriendlySize variable would now contain the string "50 KB".

Conclusion

Hopefully, this Quickfire article has given you a brief understanding of how to display file sizes in a human-readable format in your Laravel applications.

If you enjoyed reading this post, you might be interested in checking out my 220+ page ebook "Battle Ready Laravel" which covers similar topics in more depth.

Or, you might want to check out my other 440+ page ebook "Consuming APIs in Laravel" which teaches you how to use Laravel to consume APIs from other services.

If you're interested in getting updated each time I publish a new post, feel free to sign up for my newsletter.

Keep on building awesome stuff! 🚀

Last updated 2 months ago.

driesvints, ostap liked this article

2
Like this article? Let the author know and give them a clap!
ash-jc-allen (Ash Allen) I'm a freelance Laravel web developer from Preston, UK. I maintain the Ash Allen Design blog and get to work on loads of cool and exciting projects 🚀

Other articles you might like

Article Hero Image June 30th 2025

Check if a Signed URL is Valid in Laravel Tests

Introduction There may be times when you want to check whether a URL in your Laravel application is...

Read article
Article Hero Image June 22nd 2025

Pass a Query Builder to "whereIn" to Reduce Your DB Queries

Introduction I recently learnt about a cool feature in Laravel that allows you to pass a query build...

Read article
Article Hero Image June 14th 2025

Asymmetric Property Visibility in PHP

Introduction Asymmetric visibility is a feature that was introduced in PHP 8.4 (released: November 2...

Read article

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.