Laravel get array of filenames from folder

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Extracting Just Filenames: A Laravel Guide to File System Navigation

I am getting an array of filenames from a folder like this:

$files = File::allFiles('myfolder/');

But the resulting array contains the full pathnames as well.

Is there a way of just getting an array of filenames? Or do I need to process each array item and extract the filename from it?


As developers working with file systems, understanding how operating system paths are structured is crucial. When you interact with directories via functions like File::allFiles() in Laravel, you are dealing with full system paths, which often leads to unnecessary complexity if all you need is the clean filename.

The short answer is: Yes, you must process the results to extract just the filenames. Relying on the raw output from file system calls will not give you the clean array you are looking for. However, fortunately, PHP provides excellent built-in functions that make this extraction trivial and highly efficient.

The Pitfall of Full Paths

When you use methods like File::allFiles() or standard PHP functions like scandir(), they return the complete path to every file within the specified directory. For instance, if your folder is /var/www/app/public/images/, the resulting array will contain entries like /var/www/app/public/images/photo1.jpg.

While this information is useful for operations requiring full paths (like moving or deleting a file), it’s often redundant when you simply need to iterate over and process the names of the files themselves. Trying to use these full paths in subsequent logic can lead to bugs, especially when dealing with relative vs. absolute path concerns within a Laravel application structure.

The Solution: Using basename() for Clean Extraction

The most robust and idiomatic way to solve this problem in PHP is by leveraging the built-in basename() function. This function is specifically designed to extract the final component of a path, which, in the context of a file system call, is exactly what we need—the filename.

Here is how you can refactor your code to achieve a clean array of filenames:

use Illuminate\Support\Facades\File;

$folderPath = 'myfolder/';

// 1. Get the full list of files (including paths)
$fullPaths = File::allFiles($folderPath);

// 2. Process the results using array_map and basename()
$filenames = array_map(function ($path) {
    // Use basename() to strip the directory path, leaving only the filename
    return basename($path);
}, $fullPaths);

// $filenames will now contain an array of just filenames, e.g., ['file1.jpg', 'image2.png']

Deeper Dive into Best Practices

Using array_map() combined with basename() is a highly efficient approach because it processes the entire array in a single operation without requiring explicit looping constructs like foreach. This aligns perfectly with the functional programming style often encouraged in modern Laravel development, emphasizing clean, expressive code.

For scenarios where you are dealing with recursive directory structures or need more complex path manipulation (like splitting paths into directories and extensions), exploring dedicated libraries or deeper PHP functions like pathinfo() can also be beneficial. For instance, pathinfo($path, PATHINFO_FILENAME) achieves a similar goal but is slightly more verbose than the direct simplicity of basename().

When building robust file management systems within Laravel, ensuring you handle these path details correctly is key to maintaining application integrity. As we explore deeper aspects of how Laravel interfaces with the underlying operating system and file structure, understanding these low-level details becomes increasingly important for senior developers working on complex projects. You can find more insights into structuring your application effectively on the official site of laravelcompany.com.

Conclusion

To summarize, while File::allFiles() gives you the full context of file locations, it doesn't directly provide just the filenames. The most practical solution is to pipe the results through a mapping function that utilizes PHP’s native basename() function. This ensures your resulting array contains only the clean filenames, making your subsequent application logic cleaner, more predictable, and less error-prone. Always think about what data you actually need when interacting with file system operations!