ItSolutionStuff.com

How to Get Size of Directory in MB Laravel?

By Hardik Savani • April 16, 2024
Laravel

Mostly we require to calculate for uploaded directory size because we can see how much we size allocate in uploaded directory. If you have limited server then you always need to check how much size of data in my folder.

you can also use in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application.

You can also get size of directory that way you can also display on your admin panel. Laravel provide File facade. File facade through we can get folder size in kb or mb or gb etc.

In bellow example i have images directory in public folder. so we can get size for images folder like this way :

Example:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use File;

class ItemController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$size = $this->getFolderSize(public_path('images'));

dd($size);

}

/**

* Write code on Method

*

* @return response()

*/

public function getFolderSize($path)

{

$fileSize = 0;

foreach( File::allFiles($path) as $file){

$fileSize += $file->getSize();

}

return number_format($fileSize / 1048576,2);

}

}

I hope it cab help you...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to use Model Events in Laravel 8?

Read Now →

Laravel Eloquent inRandomOrder() Method Example

Read Now →

Laravel Eloquent whereRaw Condition Example

Read Now →

How to Delete File from Public Folder / Storage Folder in Laravel?

Read Now →

Laravel Move File from One Folder to Another Example

Read Now →

Laravel Copy File from One Folder to Another Example

Read Now →

How to Display Image from Storage Folder in Laravel?

Read Now →

Laravel Relationship Eager Loading with Count Example

Read Now →

How to Get File Size from Storage in Laravel?

Read Now →

How to Use Soft Delete in Laravel?

Read Now →

Laravel Storage Dropbox Integration Example

Read Now →

How to Create Zip File in Laravel?

Read Now →

How to Count Files in a Directory using Laravel?

Read Now →

Laravel Create JSON File & Download From Text Example

Read Now →