Laravel - How to Get All Files in a Directory?
Hello,
This example is focused on how to get all files in a directory laravel. I would like to show you laravel get all files in directory. I explained simply about laravel get all file names in directory. This article goes in detailed on laravel list all files in directory.
You can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 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.
So, let's see the below examples code with output:
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
)
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
)
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Download File from URL to Storage Example
- How to Write Text on Existing PDF File in Laravel?
- How to Set Timezone in Laravel?
- How to Convert Image to Base64 in Laravel?
- How to Get Browser Name and Version in Laravel?
- How to Convert JSON to Array in Laravel?
- How to Convert Number to Words in Laravel?
- How to Get Environment Variable in Laravel React JS?
- How to use Try Catch Exception in Laravel App?
- Laravel 9 Auth with Livewire Jetstream Example
- Laravel Image Upload with Spatie's Media Library Example
- Laravel Move File from One Folder to Another Example
- How to Create Widgets in Laravel Application?