How to List Files & Folders of a Directory using PHP
Whether you’re building a file management system, creating a gallery, or processing uploaded files, PHP provides powerful functions to interact with the file system. In this tutorial you will learn how to list files in a directory using PHP.
Dummy Folder Structure:
Here is a dummy
uploads folder structure that we will use for demonstration:
uploads/
├── images/
│ ├── profile.png
│ └── cover.png
├── index.html
└── script.js
1. Set the Directory Path:
To access a directory named “uploads” located in the same directory as your PHP script, you can use relative or absolute path:
$directory = 'uploads/'; // Relative Path
# OR
$directory = '/abs/path/of/the/uploads/'; // Absolute Path
2. Use scandir() Function to Scan the Directory:
PHP’s scandir() function is a simple way to list files and directories in a given directory. It returns an array containing the names of all files and directories found in the specified directory.
<?php
$directory = 'uploads/';
$files = scandir($directory);
foreach ($files as $file) {
echo $file . "\n";
}
Output:
.
..
images
index.html
script.js
3. Filter Out the “.” and “..” Entries:
When using scandir()
, keep in mind that it includes the special entries “.” (current directory) and “..” (parent directory) in the result. You’ll often want to filter out these entries, as they are not actual files.
<?php
// Define the directory where files are stored
$directory = 'uploads/';
// Get the list of files in the directory
$files = scandir($directory);
// Loop through each file in the directory
foreach ($files as $file) {
// Exclude current directory (.) and parent directory (..)
if ($file != '.' && $file != '..') {
// Output the file name
echo $file . "\n";
}
}
Output:
images
index.html
script.js
4. List Files Only:
If you want to list only files (not directories), you can filter the results using is_file()
. The following PHP code will list only files in the “uploads” directory.
<?php
$directory = 'uploads/';
$files = scandir($directory);
foreach ($files as $file) {
$filePath = $directory . $file;
// Check if the item is a file (not a directory)
if (is_file($filePath)) {
// Output the file name
echo $file . "\n";
}
}
Output:
index.html
script.js
5. Directory and its Subdirectories Listing:
To list files in a directory and its subdirectories recursively, you can create a recursive function or use libraries like RecursiveDirectoryIterator
and RecursiveIteratorIterator
.
The PHP code given in the below will list files in the “uploads”
directory and all its subdirectories.
<?php
function listFilesRecursive($directory) {
// Create a new RecursiveDirectoryIterator to iterate through the directory recursively
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
// Loop through each item in the iterator
foreach ($iterator as $file) {
// Check if the item is a file
if ($file->isFile()) {
// Output the file path
echo $file->getPathname() . "\n";
}
}
}
// Define the directory to search for files recursively
$directory = 'uploads/';
// Call the function to list files recursively in the specified directory
listFilesRecursive($directory);
Output:
uploads\images\cover.png
uploads\images\profile.png
uploads\index.html
uploads\script.js