Get uploaded file size in Laravel 11

If you are working in Laravel and want to get the size of uploaded file, you have come to the right place. Let’s say you are developing an application where it is required to see the total file size uploaded by user. In this case, you need to keep track of all the uploaded files.

You can either loop through all files in storage and get their sizes. Or you can get the file size during upload and save it in your database. Either way, this article will help you.

In this article, we will do the following:

  1. Create an HTML form to upload file
  2. Upload file and get uploaded file size
  3. Convert bytes to readable format

1. Create an HTML form to upload file

First, we will create an HTML form to upload the file.

<!-- resources/views/upload.blade.php -->

<form method="POST" enctype="multipart/form-data" action="{{ url('/upload') }}">
    {{ csrf_field() }}

    <input type="file" name="image" accept="image/*" required />
    <input type="submit" value="Upload" />
</form>

This will create an input-type file that will allow only images. It will be a required field, the form will not be submitted until a file is selected. And a submit button labeled as “Upload”, when clicked will submit the form.

This will also create a hidden CSRF field required for Laravel form submission.

One more thing you can do here is to allow the user to preview the image before uploading. Check this tutorial if you want to know how to do it.

2. Upload file and get uploaded file size

Next step is to upload the file to storage. For this, first, we need to create a route that will send the request to our controller.

// routes/web.php

use App\Http\Controllers\UserController;

Route::post("/upload", [UserController::class, "upload"]);

After that, we need to create that method in our controller.

// app/Http/Controllers/UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
    public function upload()
    {
        //
    }
}

Inside this method, we will upload the file and get its size.

if (request()->file("image"))
{
    $image = request()->file("image");

    $file_path = "images/" . $image->getClientOriginalName();
    $image->storeAs("/public", $file_path);

    // PHP way
    $file_size = filesize(storage_path("app/public/" . $file_path));
    
    // Laravel way
    $file_size = $file->getSize();
    
    return $file_size;
}

This will return the file size in bytes. You can convert the bytes into a human-readable format by following the next step.

3. Convert bytes to readable format

Create the following method in your controller.

private function bytes_to_size($bytes)
{
    $sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
    if ($bytes == 0) return '0 Byte';
    $i = floor(log($bytes, 1024));
    return round($bytes / pow(1024, $i), 2) . ' ' . $sizes[$i];
}

You can call this function in the following way:

$file_size = $this->bytes_to_size($file_size);

That’s it. That’s how you can get the uploaded file size in Laravel 11 and also can convert the bytes in human-readable format.