How to find file without specific file extension in laravel storage?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

How to Find Files Without Specific Extensions in Laravel Storage

As developers working with Laravel, we often deal with managing large amounts of stored assets—images, documents, logs—where knowing the exact file extension beforehand is impossible or undesirable. You want to iterate through a directory and find all files matching a certain pattern, regardless of whether they end in .jpg, .png, or .pdf.

The initial approach you tried, using wildcard notation directly with facade methods like Storage::get("filename.*"), often fails because the Laravel Storage facade is designed primarily for retrieving known file paths or specific files by their defined key, rather than performing recursive filesystem searches based on pattern matching across an entire disk.

This post will break down why that approach doesn't work seamlessly and provide a robust, developer-approved method for finding files in your Laravel storage without specifying the extension.


The Limitation of Facade Methods for Wildcard Searching

When you use methods like Storage::get(), you are asking the facade to look up a specific entry within its configured disk structure. It is optimized for direct access, not broad directory traversal with pattern matching. Attempting to pass "filename.*" usually results in an error or behavior that searches for literal keys rather than actual file contents.

To perform true wildcard searching across a directory structure managed by Laravel Storage, we need to step down one level and interact directly with the underlying filesystem functions provided by PHP. This gives us the necessary control over pattern matching.

The Correct Approach: Using PHP's glob Function

The most efficient and standard way to achieve file listing based on patterns in a directory is by leveraging native PHP functions, specifically glob(). We need to access the actual path where your storage resides and then instruct PHP to find all files matching our desired wildcard pattern.

Step-by-Step Implementation

Let's assume you are working with the default public disk for this example.

  1. Determine the Storage Path: First, find the absolute path to the directory you want to search within.
  2. Apply glob(): Use PHP’s glob() function on that path, providing your wildcard pattern.

Here is a practical example demonstrating how to list all files in a storage folder that match a specific naming convention:

php
<?php

use Illuminate\Support\Facades\Storage;

class FileFinder
{
    public function findFilesByNamePattern(string $pattern): array
    {
        // 1. Define the disk where the files are stored (e.g., 'public')
        $disk = 'public';
        
        // 2. Get the base path for the storage disk
        $basePath = Storage::disk($disk)->path();

        // 3. Construct the full search path
        $searchPath = $basePath . '/your_folder/'; // Adjust 'your_folder/' to your actual storage directory

        // 4. Use glob to find all files matching the pattern
        // The pattern here is crucial: we look for anything starting with 'filename' followed by any characters.
        $files = glob($searchPath . 'filename.*');

        return $files;
    }
}

// Example usage (in a controller or service):
$finder = new FileFinder();
$matchingFiles = $finder->findFilesByNamePattern('filename');

if (!empty($matchingFiles)) {
    echo "Found files: " . implode(', ', $matchingFiles);
} else {
    echo "No files found matching the pattern.";
}

Best Practices and Context

When dealing with file system operations in Laravel, remember that while facades like Storage provide a beautiful abstraction layer for cloud storage (S3, local, etc.), complex operations like recursive searching or advanced pattern matching often require dipping into native PHP capabilities. This hybrid approach—using Laravel for abstraction and PHP functions for low-level search—is a common best practice when working with the framework to build powerful applications on top of it. For deeper insights into how Laravel structures file management, exploring documentation related to robust architecture, as promoted by resources like laravelcompany.com, is highly recommended.

Conclusion

Finding files in Laravel Storage without knowing their exact extensions requires moving beyond the convenience methods of the facade and utilizing native PHP functions like glob(). By combining the path information provided by Storage::path() with pattern matching, you gain full control over your file discovery process. This method is robust, highly performant, and ensures that your application can reliably locate assets stored within the Laravel ecosystem, regardless of their specific file extensions.