How To Check Occupied Disk Space In Laravel
2 min readDec 20, 2020
- In this example we will see how to check occupied disk space in laravel.
- Many time we need requirements to check occupied disk space of server in admin side and we are checking manually if disk space are fully occupied or not so in this tutorial i will explain you how to check occupied disk space in laravel so you can check directly in admin side without any manual action. Also you can use this code in Ubuntu as well.
- PHP provide built-in function to check total space and free space of server, here we will use these functions and will get output.
- So, first you need to create Controller for logical part.
I have created one controller and created disk_total_demo() function like below.
public function disk_total_demo()
{
$disktotal = disk_total_space('/'); //DISK usage
$disktotalsize = $disktotal / 1073741824; $diskfree = disk_free_space('/');
$used = $disktotal - $diskfree; $diskusedize = $used / 1073741824;
$diskuse1 = round(100 - (($diskusedize / $disktotalsize) * 100));
$diskuse = round(100 - ($diskuse1)) . '%'; return view('details',compact('diskuse','disktotalsize','diskusedize'));
}
Then after put below code in your details.blade.php file.
<html>
<head>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="col-sm-6 col-md-3">
<h2 class="no-margin text-semibold">Occupied Disk Space</h2>
<div class="progress progress-micro mb-10">
<div class="progress-bar bg-indigo-400" style="width: {{$diskuse}}">
<span class="sr-only">{{$diskuse}}</span>
</div>
</div>
<span class="pull-right">{{round($diskusedize,2)}} GB /
{{round($disktotalsize,2)}} GB ({{$diskuse}})</span>
</div></body>
</html>
And finally we will get output like below screenshot.