How to get files list from public folder Laravel
One of the most common requests in a Laravel application involves listing the contents of the public directory and iterating over those files. The confusion often stems from mixing up the physical file system path with Laravel's virtual storage abstraction. When you have images or assets sitting directly inside the public folderâas opposed to using the storage diskâyou are dealing with standard filesystem operations rather than Storage Facade calls.
The most direct way to achieve this is by using PHP's built-in Scandir function combined with Glob. Since Laravel provides access to the underlying filesystem via the Illuminate\Filesystem\Filesystem helper, you don't need to reinvent the wheel. You simply target the absolute path of your public directory.
Here is a practical approach to retrieving a list of files in the public folder:
use Illuminate\Support\Facades\Dir; // Or use Filesystem facade
// Option 1: Using the Filesystem Facade (Recommended for Laravel)
$files = [];
$fs = new \Illuminate\Filesystem\Filesystem();
$publicPath = public_path(''); // Returns the absolute path to base/public
// Scan directories recursively or non-recursively depending on needs
// Here we look only at files in the root of the public folder
$allContents = $fs->files($publicPath);
foreach ($allContents as $file) {
// Process your file here
// Example: Get relative path, extension, or size
$relativePath = str_replace($publicPath, '', $file);
$extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if ($extension === 'jpg' || $extension === 'png') {
// Do something specific with image files
echo "Found: {$relativePath}<br>";
}
}
If you need to handle symbolic links or ensure you are only processing actual files and not directories, the files method of the Filesystem facade handles this elegantly. It filters out directories automatically if configured correctly, but explicitly checking for file types adds safety.
Alternatively, you can use Scandir directly on the path returned by public_path(). This gives you raw entries which you must then filter:
$directory = public_path();
$contents = scandir($directory);
foreach ($contents as $item) {
if (is_dir("$directory/$item")) {
continue; // Skip directories
}
$fullPath = "$directory/$item";
// Perform your logic here
}
A critical distinction to make early on is avoiding the Storage facade unless you are specifically targeting a disk mounted in config/filesystems.php. The public folder is typically served directly by the web server (Nginx or Apache) via the document root. Using Storage::disk('public')->allFiles() will only work if you have explicitly configured a 'public' disk to point to that directory. If your images are physically located in storage/app/public, they require a symlink to storage/framework/public or a specific mount to be accessible via the web server, but that is a different architectural pattern.
When processing these files, keep performance in mind. If you have thousands of small files, iterating through them in a single PHP request can lead to memory exhaustion. In high-traffic scenarios where you need to process every file in the public folder (e.g., for analytics or cleanup tasks), consider running this logic via a scheduled task using Laravel's scheduler rather than triggering it from a user request.
Another pitfall involves relative paths versus absolute paths. Always normalize your paths before performing string manipulation or passing them to other systems like S3 or FTP. Using realpath() can help resolve symlinks if your deployment structure relies on them, ensuring consistency regardless of how the server is configured.
If you need to filter by specific extensions or date ranges, remember that Scandir returns entries in natural sort order, but it includes hidden files starting with a dot. You will need to explicitly filter those out using strpos($item, '.') === 0 before processing further.