Laravel 9 How to Get Files Information From Directory

Share this Article
Reading Time: 5 minutes
1,616 Views

Inside this article we will see the concept of Laravel 9 How to get files information from directory. 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.

Laravel Installation

Open terminal and run this command to create a laravel project.

$ composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

$ php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Analyze Public Folder

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

We can see 8 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 inside /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 9</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 9</h4>
                <div class="panel panel-primary">
                    <div class="panel-heading">How to Get Files Information From Directory in Laravel 9</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>

Add Route

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

//...

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 Laravel 9 How to Get Files Information From Directory Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

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.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5