Laravel 9 How to Get Files Information From Directory

Reading Time: 5 minutes

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


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.


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.


    <title>How to Get Files Information From Directory in Laravel 9</title>
    <link rel="stylesheet" href="">

    <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">
                                    $count = 1;
                                @foreach ($files_info as $item)
                                        <td>{{ $count++ }}</td>
                                        <td>{{ $item['filename'] }}</td>
                                        <td>{{ $item['fileext'] }}</td>
                                            {{ $item['filesize'] . ' bytes' }} /
                                            {{ round($item['filesize'] / 1000, 1) . ' KB' }} /
                                            {{ round($item['filesize'] / (1000 * 1024), 2) . ' MB' }}



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

Run this command into project terminal to start development server,

php artisan serve


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

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.