How to Get the Size of a Directory in MB in Laravel
How to Get the Size of a Directory in MB in Laravel

How to Get the Size of a Directory in MB in Laravel

In this article, we’ll walk through the process of getting the size of a directory in megabytes (MB) in a Laravel application. We’ll use simple and straightforward steps, ensuring that everything is easy to understand.

Step 1: Setting Up Laravel

First, if you haven’t already set up a Laravel project, you’ll need to create one. Open your terminal and run the following command:

composer create-project --prefer-dist laravel/laravel directory-size

Navigate to your Laravel project directory:

cd directory-size

Step 2: Creating a Helper Function

We’ll create a helper function to calculate the size of a directory. Laravel doesn’t have a built-in function for this, so we’ll write our own.

  1. Create a new helper file: Create a file named helpers.php in the app directory.
   touch app/helpers.php
  1. Write the function: Open the helpers.php file and add the following code:
   <?php

   function getDirectorySize($path)
   {
       $size = 0;

       foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $file) {
           $size += $file->getSize();
       }

       return $size;
   }

   function formatSizeUnits($bytes)
   {
       $units = array('B', 'KB', 'MB', 'GB', 'TB');

       for ($i = 0; $bytes >= 1024 && $i < count($units) - 1; $i++) {
           $bytes /= 1024;
       }

       return round($bytes, 2) . ' ' . $units[$i];
   }
  1. Include the helper file: To make sure Laravel loads this helper file, open the composer.json file and add it to the autoload section:
   "autoload": {
       "files": [
           "app/helpers.php"
       ]
   }
  1. Update Composer: Run the following command to update Composer’s autoload files:
   composer dump-autoload

Step 3: Creating a Controller

Next, we’ll create a controller to use our helper function.

  1. Create a new controller: Run the following command to create a controller named DirectoryController:
   php artisan make:controller DirectoryController
  1. Add the function to the controller: Open the newly created DirectoryController.php file in app/Http/Controllers and add the following code:
   <?php

   namespace App\Http\Controllers;

   use Illuminate\Http\Request;

   class DirectoryController extends Controller
   {
       public function getSize()
       {
           $directoryPath = base_path(); // You can change this to any directory path you want
           $sizeInBytes = getDirectorySize($directoryPath);
           $sizeInMB = formatSizeUnits($sizeInBytes);

           return view('directory-size', compact('sizeInMB'));
       }
   }

Step 4: Creating a Route

Now, we need to create a route to access our controller method.

  1. Open the web.php file: Open the routes/web.php file and add the following route:
   use App\Http\Controllers\DirectoryController;

   Route::get('/directory-size', [DirectoryController::class, 'getSize']);

Step 5: Creating a View

Finally, let’s create a simple view to display the size of the directory.

  1. Create a new view file: Create a file named directory-size.blade.php in the resources/views directory.
   touch resources/views/directory-size.blade.php
  1. Add the following code: Open the directory-size.blade.php file and add the following code:
   <!DOCTYPE html>
   <html>
   <head>
       <title>Directory Size</title>
   </head>
   <body>
       <h1>Directory Size</h1>
       <p>The size of the directory is: {{ $sizeInMB }}</p>
   </body>
   </html>

Step 6: Testing

Now, you can test the functionality by starting your Laravel server and visiting the route.

  1. Start the Laravel server: Run the following command to start the server:
   php artisan serve
  1. Visit the route: Open your web browser and visit http://localhost:8000/directory-size. You should see the size of the directory displayed in megabytes.

Conclusion

In this article, we learned how to get the size of a directory in megabytes in a Laravel application. We created a helper function, set up a controller, added a route, and created a view to display the directory size. This approach can be extended and customized to fit various needs in your Laravel projects.