How to Get Size of Directory in MB Laravel?

Today now in this post i will show you how to Get Size of Directory in MB Laravel? Mostly we need to calculate for the uploaded directory size because we can see that how much we size allocate in uploaded directory. If we have any limited server then we can always need to check how much size of data in our folder.

we can also use in any version laravel 6, laravel 7 and laravel 8 application.

We can also get the size of directory on that way we can also display on our admin panel. In Laravel provide us File facade. File facade through we can get the folder size in kb or mb or gb etc.

Now in the bellow example i have an images directory in public folder. so we need to get size for images folder as like on this way :

$file_size = 0;
foreach( File::allFiles(public_path('images')) as $file)
    $file_size += $file->getSize();
$file_size = number_format($file_size / 1048576,2);
print_r($file_size.' MB');

Read Also: How to convert file extension using CloudConvert in laravel?

Thanks for read. I hope it help you. For more you can follow us on facebook

Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.