How to Get All Files in a Directory in Laravel (2022)

Hello dev, today we are going to learn How to Get All Files in a Directory in Laravel. This tutorial will cover on getting all files in a directory/folder in laravel application.

You can use this example with laravel 6, laravel 7, laravel 8 and laravel 9 versions.

There are several ways to get a list of all files in a directory using laravel php code. I will give you some examples of code to get all files from the folder. we will use laravel Storage facade to get files.

Also Read: How to Get All Routes in Laravel 9 ? [ New / Free Tutorials ]

Examples on How to Get All Files in a Directory in Laravel

Example 1: Laravel Get All Files of Specific Directory

Here, we will use files() function of Storage facade. it will return all files of specific folder.

app/Http/Controllers/DemoController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
  
class DemoController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $directory = "public";
        $files = Storage::files($directory);
  
        dd($files);
    }
}

Output:

Array
(
    [0] => public/output.pdf
    [1] => public/.gitignore
    [2] => public/test.pdf
)

Also Read: How to Truncate String in Laravel?

Example 2: Laravel Get All Files of Directory Recursively

Here, we will use allFiles() function of Storage facade. it will return all files of specific folder with subdirectories files as well.

app/Http/Controllers/DemoController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
  
class DemoController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $directory = "public";
        $files = Storage::allFiles($directory);
  
        dd($files);
    }
}

Output:

Array
(
    [0] => public/output.pdf
    [1] => public/uploads/627653f563dde.png
    [2] => public/uploads/demo2.txt
    [3] => public/uploads/demo3.txt
    [4] => public/.gitignore
    [5] => public/test.pdf
)

Also Read: How to Convert Image to Base64 in Laravel?

Laravel File Storage

Laravel provides a powerful filesystem abstraction thanks to the wonderful Flysystem PHP package by Frank de Jonge. The Laravel Flysystem integration provides simple drivers for working with local filesystems, SFTP, and Amazon S3. Even better, it’s amazingly simple to switch between these storage options between your local development machine and production server as the API remains the same for each system.

Configuration

Laravel’s filesystem configuration file is located at config/filesystems.php. Within this file, you may configure all of your filesystem “disks”. Each disk represents a particular storage driver and storage location. Example configurations for each supported driver are included in the configuration file so you can modify the configuration to reflect your storage preferences and credentials.

The local driver interacts with files stored locally on the server running the Laravel application while the s3 driver is used to write to Amazon’s S3 cloud storage service.

Also Read: How to Get Browser Name and Version in Laravel 9?

Conclusion

Today, We had learn How to Get All Files in a Directory in Laravel. Hope this tutorial helped you with learning Laravel 9. If you have any question you can ask us at comment section below. If you like the tutorial please subscribe our YouTube Channel and follow us on social network Facebook and Instagram.

Question Asked

  • How do I get files in Laravel?
  • How can I get storage path in Laravel 8?
  • How do I download from storage in Laravel?
  • How do I move files in Laravel?
  • How to Get All Files in a Directory?
  • Get All Files in a Directory?

Also Read: Laravel 9 Custom Login and Registration Example

Leave a Reply