How To Check Occupied Disk Space In Laravel

In this tutorial I will give you example about how to check occupied disk space in laravel or check free RAM in server. Many time we need requirments to check occupied disk space of server in adminside dashboard and we are checking manually if disk space are fully occipied or not. So, In this tutorial I will explain you how to check occupied disk space in laravel so you can check directly in adminside 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 disk_total_space() and disk_free_space() functions and will get output.

The disk_total_space() function returns the total size, in bytes, of the specified filesystem or disk.

Syntax :

disk_total_space(directory_name)

The disk_free_space() function returns the free space, in bytes, of the specified filesystem or disk.

 

 

Syntax :

disk_free_space(directory_name)

 

Example : 

 I have created one controller and created disk_occupied() function like below.

public function disk_occupied()
{
        $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('home',compact('diskuse','disktotalsize','diskusedize'));
}

Then after put below code in your home.blade.php file.

<html>
<head>
  <link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <h3 class="no-margin text-semibold text-center">Occupied Disk Space - Techsolutionstuff</h3>
    <div class="col-sm-12 col-md-4 col-md-offset-4">    
        <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.

Occupied Disk Space

You might also like : 

FEATURE POSTS