Home / Snippets / How to Get All Files Within a Directory in Laravel
How to Get All Files Within a Directory in Laravel cover

How to Get All Files Within a Directory in Laravel

1.2K

3 years ago

0 comments

To get all of the files within a directory you can make use of the "files()" method available from the "Storage" facade. Running the code below will return an array of the files within the directory you have specified.
<?php

$files = Storage::files($directory);
If within the directory there are subdirectories then it will be listed as a directory, to get all of the files within the subdirectory you can call the "allFiles()" method.
<?php

$files = Storage::allFiles($directory);
Do note that it supports listing nested directories.
<?php

Storage::allFiles('path/to/directory/);

How to specify Disk instance used?


Since some Laravel applications will have multiple disks, you can specify the disk to use like below.
<?php

Storage::disk('s3')->allFiles('path/to/directory/); // if you are useing 's3' disk
Storage::disk('public')->allFiles('path/to/directory/); // if you are using 'public' disk
Do note that by default it will be using the one specified within the .env file like below.
# .env
FILESYSTEM_DRIVER=local
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this