How to Get Files Information From Directory in Laravel 8

Reading Time: 5 minutes
8,863 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.


Laravel Installation

We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.

Here is the command to create a laravel project-

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

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 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 this route into it.

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

//...

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

Application Testing

Run this command into project terminal 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.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more