Use Laravel 10 To Get Files Information From Directory

Reading Time: 5 minutes
786 Views

This tutorial will show you How To Use Laravel 10 To Get Files Information From Directory. Working with files efficiently is a common requirement in web application development for Laravel developers.

In this tutorial, we will walk you through the process of using Laravel 10 to access detailed information about files placed in a directory. Laravel 10 provides straightforward and efficient ways for retrieving file names, file sizes, file formats, and other file-related activities.

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.

Read More: How To Clear Complete Application Caches in Laravel 10

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 (Files Analysis)

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

We can see 7 images available in images folder.

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

Create Application Controller (File)

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.

Read More: How To Create and Download Zip File in Laravel 10 Tutorial

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

<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\File;
use Illuminate\View\View;

class FileController extends Controller
{
    public function index(): View
    {
        $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 10</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 10</h4>
                <div class="panel panel-primary">
                    <div class="panel-heading">How To Get Files Information From Directory in Laravel 10</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 Illuminate\Support\Facades\Route;
use App\Http\Controllers\FileController;

Route::get('files-detail', [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/files-detail

We hope this article helped you to learn Use Laravel 10 To Get Files Information From Directory Tutorial in a very detailed way.

Read More: How To Query To Get Single Row Data in Laravel 10 Tutorial

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

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.