Human-Readable File Sizes in Laravel (KB, MB, GB)
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! ?
Other articles you might like
Laravel 12 Custom Validation Rules Example
In this Laravel tutorial titled “laravel 12 custom validation rules example”, you will learn how to...
Returning HTTP 404 Responses Instead of 403 for Unauthorised Access
Introduction When building a web application, you typically add authorisation checks to ensure that...
Run PHPUnit and Pest Tests Without Vite Assets in Laravel
Introduction A common way to build your Laravel application's frontend assets is with Vite (by runni...
The Laravel portal for problem solving, knowledge sharing and community building.