How To Check Occupied Disk Space In Laravel

Websolutionstuff | Jul-29-2020 | Categories : Laravel PHP

Many times we need requirements to check the occupied disk space of the server on the admin side and we are checking manually if the disk space is fully occupied or not. So, in this tutorial, I will explain to you how to check occupied disk space in laravel. So, you can check directly on the admin side without any manual action. Also, you can use this code in ubuntu as well.

PHP provides a built-in function to check the total space and free space of the server, here we will use these functions to check server space.

first, you need to create a controller for the logical part of free disk space. The disk_free_space() function returns the free space, in bytes, of the specified filesystem or disk. The disk_total_space() function returns the total size, in bytes, of the specified filesystem or disk.

 I have created one controller and created the 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 put the 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 the below screenshot.

occupied disk space
 

You might also like :

Recommended Post
Featured Post
StartOf And EndOf Functions Example Of Carbon
StartOf And EndOf Functions Ex...

In this article, we will see startof and endof functions example of carbon in laravel. As you all know carbon provide ma...

Read More

Dec-19-2020

How To Change Text In Laravel 9 Datatable
How To Change Text In Laravel...

Do you want to learn how to change text in a Laravel 9 Datatable? This blog post is your complete guide to mastering tex...

Read More

Jan-06-2023

PHP - file_get_contents() SSL Connection Reset by Peer
PHP - file_get_contents() SSL...

Hey there! So, you're working with PHP and trying to fetch content from an HTTPS URL using the file_get_contents() f...

Read More

Mar-01-2024

Laravel 8 Image Upload Validation
Laravel 8 Image Upload Validat...

In tutorial we will see how to validate laravel 8 image upload validation. In laravel 7/8 you can validate image using t...

Read More

Dec-15-2021