In Laravel, how can I obtain a list of all files in a public folder?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Title: Obtaining a List of All Files in Laravel's Public Folder

Introduction: In Laravel, you may find it difficult to obtain a list of all files within the public folder. However, with thorough understanding and effective techniques, you can easily achieve this task. This article will guide you through different methods to accomplish your goal while incorporating best practices for future use cases.

  1. Using SplFileInfo: The most straightforward approach is utilizing the native PHP SplFileInfo class within Laravel's context. This class provides various file system operations, including listing files in a directory and accessing their basic properties. To list all files in your public folder:
php
use Illuminate\Support\Facades\Storage;

$list = Storage::disk('public')->files();
foreach ($list as $file) {
    // Process each file here
}

In this example, you're accessing the public folder using Laravel's Facade Storage, which is a wrapper around the underlying PHP library. By calling the files() method, it returns an array of all files in the specified directory along with their paths. Each file can then be iterated through and processed as needed.

  1. Exploring Recursive Directory Iterators: Another approach could be using a recursive directory iterator to traverse the public folder. Laravel provides a Facade called App\Filesystem for this purpose. Here's how you can implement it:
php
use Illuminate\Support\Facades\Storage;
use FilesystemIterator;

$iterator = new FilesystemIterator(public_path(), FilesystemIterator::SKIP_DOTS);
foreach ($iterator as $file) {
    // Process each file here
}

In this example, you're using the FilesystemIterator class to iterate through all files within your public folder. The public_path() function returns the path to the public directory from Laravel's root namespace, and the SKIP_DOTS flag is used to ignore hidden folders such as ".git" or "node_modules." You can then process each file as needed by iterating through the iterator.

  1. Leveraging Laravel Collections: For a more advanced approach, you could utilize Laravel's powerful Collection class to handle complex data structures. Here's an example:
php
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Collection;

$collection = Storage::disk('public')->files()
                    ->map(function ($file) {
                        // Process each file here and return it as a new array
                    })->values()->toArray();

In this example, you're using Laravel's Collection class to chain operations. First, you obtain the list of files in your public folder by calling files() on the Storage Facade. Using map(), you can apply a function to process each file and return an array containing the results. Finally, you call toArray() to convert it back into a plain PHP array for further manipulation or processing.

Conclusion:
In summary, there are multiple ways to achieve your goal of obtaining a list of all files within Laravel's public folder. Depending on the specific requirements and preferences, one may choose between using SplFileInfo, recursive directory iterators, or leveraging Laravel Collections. Always ensure you maintain good coding practices by incorporating readability, abstraction, and extensibility into your code. For more insights and tips on working with Laravel, visit https://laravelcompany.com/blog/.