How to get files list from public folder Laravel
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Efficiently Retrieving Files from Laravel's Public Folder
Introduction:
In your Laravel application, you have stored several images within the public folder (NOT storage), which is a default location for serving static assets like images and stylesheets. You want to iterate through these files and perform an action on each one. This blog post will guide you step-by-step on how to achieve this task efficiently using Laravel's built-in functionality.
Body:
1. Getting the file names from the public folder: First, we need a list of all filepaths within your public directory. To accomplish that, you can use Laravel's `File` facade with its `files` method. The following code will return an array containing all the files in the public directory:
```php
$filePaths = File::files('public');
```
2. Processing the list of file paths: To work with each image file, you can loop through this collection and perform your desired actions on each element. You may need to open each file for further processing or just use its name. Here's an example where we simply echo the filename for each image in the folder:
```php
foreach ($filePaths as $fileName) {
echo $fileName . '
'; } ``` 3. Handling nested directories within public: In some cases, you may have nested folders inside your `public` directory containing images. To handle this scenario, first, we can use a recursive function to traverse the directory structure and find all files. Then, modify the code snippet above to include nested subdirectories: ```php function getFilesWithRecursion($directory) { $files = File::files($directory); foreach ($files as $filePath) { if (File::isDir($filePath)) { $paths = getFilesWithRecursion($filePath . '/'); array_merge($files, $paths); } } return $files; } $nestedFilePaths = getFilesWithRecursion('public/'); ``` 4. Optimizing code for performance: If you have a large number of files in the `public` directory, it may take quite some time to process them all. To optimize the process and avoid memory issues, we can use Laravel's `ChunkIterator` helper. This will divide the filepaths into smaller batches, allowing us to iterate through them sequentially: ```php $chunkedFilePaths = ChunkIterator::chunk($filePaths, 10); foreach ($chunkedFilePaths as $chunk) { foreach ($chunk as $fileName) { echo $fileName . '
'; } } ``` Conclusion: In this blog post, we have discussed how to retrieve files from Laravel's public folder and perform an action on each one. By using Laravel's built-in functionality and optimizing our code, we can handle a large number of files efficiently. Remember to always handle nested directories with care, and consider using Laravel's ChunkIterator for better performance when dealing with large file collections. If you need additional assistance or have any questions about handling files in Laravel applications, feel free to reach out to the experts at https://laravelcompany.com.
'; } ``` 3. Handling nested directories within public: In some cases, you may have nested folders inside your `public` directory containing images. To handle this scenario, first, we can use a recursive function to traverse the directory structure and find all files. Then, modify the code snippet above to include nested subdirectories: ```php function getFilesWithRecursion($directory) { $files = File::files($directory); foreach ($files as $filePath) { if (File::isDir($filePath)) { $paths = getFilesWithRecursion($filePath . '/'); array_merge($files, $paths); } } return $files; } $nestedFilePaths = getFilesWithRecursion('public/'); ``` 4. Optimizing code for performance: If you have a large number of files in the `public` directory, it may take quite some time to process them all. To optimize the process and avoid memory issues, we can use Laravel's `ChunkIterator` helper. This will divide the filepaths into smaller batches, allowing us to iterate through them sequentially: ```php $chunkedFilePaths = ChunkIterator::chunk($filePaths, 10); foreach ($chunkedFilePaths as $chunk) { foreach ($chunk as $fileName) { echo $fileName . '
'; } } ``` Conclusion: In this blog post, we have discussed how to retrieve files from Laravel's public folder and perform an action on each one. By using Laravel's built-in functionality and optimizing our code, we can handle a large number of files efficiently. Remember to always handle nested directories with care, and consider using Laravel's ChunkIterator for better performance when dealing with large file collections. If you need additional assistance or have any questions about handling files in Laravel applications, feel free to reach out to the experts at https://laravelcompany.com.