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

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

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Listing Files in the Laravel Public Folder

Developers often face a common constraint: they need to generate a manifest of assets located within public/storage or directly inside the public directory itself. While the Storage facade is the standard tool for interacting with filesystems, its default behavior assumes you are working against the storage disk configuration, which typically points to an abstracted path rather than the raw public folder. Consequently, calling Storage::disk('public')->allFiles() will not give you the list of files you expect if your disk configuration maps public to a different physical location.

To obtain an accurate list of all files in the actual public folder, you must bypass the abstraction layer and interact directly with the filesystem or configure your disks correctly. The most reliable approach involves using Laravel's built-in Storage facade but ensuring the disk name corresponds to the root of your public directory. By default, Laravel creates a public disk that maps to the public folder. You can verify this by checking your config/filesystems.php. If the root for the public disk is set to /public, then Storage::disk('public')->allFiles() will return exactly what you need.

However, relying on configuration alone can be brittle if different environments have varying setup requirements. A more robust method that works regardless of disk configuration is to use PHP's native FilesystemIterator directly on the specific directory path. This approach treats the filesystem as a collection of items rather than an abstracted storage driver. You can instantiate a new SplFileInfo object for the public folder and iterate through it using FilesystemIterator::CURRENT_DIR.

Here is a practical implementation pattern you can use in a Service class or a Controller:

use SplFileInfo;
use SplFilesystem;

public function getPublicFiles(): array
{
    $files = [];
    
    // Ensure we are looking at the public folder relative to the project root
    $path = base_path('public');
    
    if (is_dir($path)) {
        $iterator = new SplFileInfo($path, true);
        
        foreach ($iterator->getChildren() as $file) {
            // Only include regular files, skipping subdirectories
            if ($file->isFile()) {
                $files[] = [
                    'relative_path' => $file->getPathname(),
                    'extension'    => $file->getExtension(),
                    'size'         => $file->getSize(),
                    'last_modified'=> $file->getMTime(),
                ];
            }
        }
    }
    
    return $files;
}

This code snippet provides granular control. By checking is_dir first, you prevent errors if the folder doesn't exist in non-development environments where symlinked storage might be missing. The loop explicitly filters for files only, ignoring subdirectories unless you specifically want to recurse into them. Using $iterator->getChildren() gives you a flat list of immediate contents, which is usually sufficient for asset generation tasks like image galleries or file listings.

If you prefer the Storage facade for consistency across your codebase, ensure your disk configuration aligns with your intent. You might define a custom disk in config/filesystems.php that explicitly points to the public folder:

'disks' => [
    'public-files' => [
        'driver' => 'local',
        'root'   => storage_path('app/public'), // Or base_path('public') depending on needs
    ],
],

Using a named disk like public-files allows you to call Storage::disk('public-files')->allFiles() without worrying about the default public disk's configuration. This separation of concerns is valuable when your application logic needs to distinguish between files intended for direct public access and those managed by Laravel's storage abstraction.

A potential pitfall with both methods is handling symbolic links. If you have symlinked your storage directory to public/storage, iterating directly on the public folder will show the symlinks as separate directories unless you filter them out. When using the native iterator, you can check $file->isLink() to exclude these from your list if they are not actual files. With the Storage facade, the driver usually resolves symlinks automatically, but this depends on how the disk is mounted in your environment.

Ultimately, the choice between using the Storage facade and native PHP iterators comes down to whether you need abstraction or raw control. For most asset listing tasks, the native iterator offers a straightforward way to inspect the actual contents of the public folder without relying on potentially ambiguous disk configurations.

Our network