How to Get Files Information From Directory in Laravel 8

Share this Article
Reading Time: 5 minutes
612 Views

Inside this article we will see the concept of How to get files information from directory in laravel 8. In laravel application we store public accessible files inside /public folder.

By this tutorial you will get the idea about finding file information from any directory inside laravel application. In a folder we store files of any type like image, pdf, doc etc.

File information like it’s name, size, extension etc. This tutorial will use laravel Filesystem concept to get file details. Tutorial will be super easy to understand and implement.

Learn More –

Let’s get started.


Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project. If your system doesn’t has Composer Installed, Learn Composer Installation Steps. Here is the complete command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel already installed at system.


Analyze Public Folder

Suppose in a laravel application we have few images into /public/images folder.

We can see 4 images available in images folder.

Let’s find the file information of these in laravel.


Create Controller

Next, we need to create a controller file.

$ php artisan make:controller FileController

It will create a file FileController.php at /app/Http/Controllers folder.

Open FileController.php and write this complete code into it.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use File;

class FileController extends Controller
{
    public function index()
    {
        $files_info = [];

        foreach (File::allFiles(public_path('images')) as $file) {

            $files_info[] = array(
                "filename" => $file->getFilename(),
                "filesize" => $file->getSize(), // returns size in bytes
                "fileext" => $file->getExtension()
            );
        }

        return view('file-details', [
            "files_info" => $files_info
        ]);
    }
}
  • File::allFiles(public_path(‘images’)) Returns all files from /public/images folder.
  • $file->getFilename() Returns file name
  • $file->getSize() Returns file size in bytes
  • $file->getExtension() Returns file extension

Create Blade Layout File

Go to /resources/views folder and create a file with name file-details.blade.php

Open file-details.blade.php and write this complete code into it.

<html>

<head>
    <title>How to Get Files Information From Directory in Laravel 8</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>

<body>
    <div class="container" style="margin-top: 30px;">
        <div class="row">
            <div class="col-md-12 offset-md-3 mt-5">
                <h4>How to Get Files Information From Directory in Laravel 8</h4>
                <div class="panel panel-primary">
                    <div class="panel-heading">How to Get Files Information From Directory in Laravel 8</div>
                    <div class="panel-body">
                        <table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>#ID</th>
                                    <th>#Filename</th>
                                    <th>#Extension</th>
                                    <th>#Size</th>
                                </tr>
                            </thead>
                            <tbody>
                                @php
                                    $count = 1;
                                @endphp
                                @foreach ($files_info as $item)
                                    <tr>
                                        <td>{{ $count++ }}</td>
                                        <td>{{ $item['filename'] }}</td>
                                        <td>{{ $item['fileext'] }}</td>
                                        <td>
                                            {{ $item['filesize'] . ' bytes' }} /
                                            {{ round($item['filesize'] / 1000, 1) . ' KB' }} /
                                            {{ round($item['filesize'] / (1000 * 1024), 2) . ' MB' }}
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

</body>

</html>

Create Route

Open web.php from /routes folder and add these route into it.

# Add this to header
use App\Http\Controllers\FileController;

//...

Route::get('file-details',[FileController::class, 'index']);

Application Testing

Open project to terminal and type the command to start development server

$ php artisan serve

URL: http://127.0.0.1:8000/file-details

We hope this article helped you to How to Get Files Information From Directory in Laravel 8 Tutorial in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on Laravel 8 Articles here