Skip to main content

Php List Files in Directory and Subdirectory Recursively

Listing Directory and subdirectory are one of the most command activity in the file handling. This is because sometimes we want to operate on all or specific files and as an input, we get the directory only. In this tutorial of PHP list files in directory, I will take you through different methods of getting all file of a specific directory.

PHP Provides different functions and feature by which you can list files in the directory. First, we will cover all different methods of listing files in the directory and then we will do the performance comparison.

In this post(PHP  List files in directory) I will only demonstrate different ways of getting files from the directory. If you want details of file handling then you can refer to File Handling PHP tutorial.

Following are the different methods/functions in PHP by which you can list files in the directory.

  1. glob function
  2. readdir function
  3. scandir function
  4. DirectoryIterator Class

Php List Files in Directory using Glob function

We can list files of any directory by using PHP function glob. PHP glob function takes pattern and an example and lists all files of the specific directory on the basis of the pattern.

PHP Glob() Function

Below is the function signature of the glob() function:
glob(Pattern , [Flag=0])

Pattern: Pattern is a mandatory parameter which excepts the pattern on the basis of which searching will be performed.

Flag: Flag is a non-mandatory parameter and by default, it will be set to zero. Below is few valid constants you can pass as a flag in glob function:

  1. GLOB_MARK: To add a slash to the each pattern matched file returned via glob.
  2. GLOB_NOSORT : If you will pass GLOB_NOSORT then all files will be returned without the sorting.
  3. GLOB_NOCHECK: Will return the search pattern if no matching will be found.
  4. GLOB_ONLYDIR: To get the only directory of the matching pattern.
  5. GLOB_ERR: To stop the pattern matching when any error like permission occur.

Basic Example of Listing file of directory using glob

Suppose, you want to list file of any directory of your WordPress then you can use the php glob function like below(example1):
<?php
$files = glob('/Applications/XAMPP/xamppfiles/htdocs/wordpress/*');
print_r($files);
?>

In the above code, we have used the pattern “/Applications/XAMPP/xamppfiles/htdocs/wordpress/*” to get all files and directories in your WordPress folder using glob function. The output of the above code for me is like below.
PHP List Files in Directory GlobList Directory Recursive Using Glob(example2):
<?php
$path = '/Applications/XAMPP/xamppfiles/htdocs/wordpress/*';
function recursive_glob($pattern) {
$first_files = glob($pattern);
foreach (glob(dirname($pattern).'/*') as $dir) {
$first_files = array_merge($first_files, recursive_glob($dir.'/'.basename($pattern)));
}
return $first_files;
}
print_r(recursive_glob($path));
?>

The above php code will list all files and directorories of my wordpress folder.

Php List Files in Directory using readdir function

Readdir is a PHP function which accpet directory handle as a parameter and returns the name of all files and directories. So using the readdir function we can get all files and folder of one directory in php.

Below is the code for listing the files and folder of wordpress directory(example3):
<?php
$path = '/Applications/XAMPP/xamppfiles/htdocs/wordpress';
function recursive_read_dir($path) {
$files = array();
if ($handle = opendir($path)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") { //Just removed . and ..
if (is_dir($path . DIRECTORY_SEPARATOR . $entry)) {
$files[$entry] = recursive_read_dir($path . DIRECTORY_SEPARATOR . $entry);
} else {
$files[] = $entry;
}
}
}
closedir($handle);
}
return $files;
}
print_r(recursive_read_dir($path));
?>

Above code will print an associative array of directories and subdrectories recursively.

PHP List File and Directory using scandir function

Scandir function in php takes directory path as input and returns list of files and directory as an output. Scandir function is simillar to readdir function but the main differnce that scandir takes directory path as an input however readdir takes directory handle as input. Also scandir accepts optional second argument for sorting in accesing or decessing order.

Below is the code for getting recursive files and directories of my wordpress folder using scandir function(example4).

<?php
$path = '/Applications/XAMPP/xamppfiles/htdocs/wordpress';
function recursive_scan_dir($path) {
$files = array();
$entries = scandir($path);
foreach ($entries as $entry) {
if ($entry != "." && $entry != "..") { //Just removed . and ..
if (is_dir($path . DIRECTORY_SEPARATOR . $entry)) {
$files[$entry] = recursive_scan_dir($path . DIRECTORY_SEPARATOR . $entry);
} else {
$files[] = $entry;
}
}
}
return $files;
}
print_r(recursive_scan_dir($path));
?>

Above code will print an associative array of directories and subdrectories recursively.

PHP List File and Directory using RecursiveDirectoryIterator

RecuesiveDirectoryIterator is a Standard PHP Library(SPL) class which provides a way to iterate recursively in the directory and do the operations. You can get the details Class Synopsis of Recursive Directory Iterator on PHP Manual. Using RecursiveDirectoryIterator class we can list files and directories in any folder using your PHP script. Below is a very simple code to print the list of directories and subdirectories recursively using RecursiveDirectoryIterator class.

Below code will list all files of the directory recursively but in a simple array(example5):
<?php
$path = '/Applications/XAMPP/xamppfiles/htdocs/wordpress';
$obj_rdi = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
foreach ($obj_rdi as $file){
$entry = $file->getfilename();
if ($entry != "." && $entry != ".."){
$file_list[] = $entry;
}
}
print_r($file_list);
?>

In the above code we have alow used another SPL class RecursiveIteratorIterator for getting recursive iterator on RecursiveDirectoryIterator.

If you want an associative array of the file then you might use the below code(example6):
<?php
$path = '/Applications/XAMPP/xamppfiles/htdocs/wordpress';
function recursive_list($path) {
$obj_rdi = new RecursiveDirectoryIterator($path);
$files = array();
foreach ($obj_rdi as $file) {
if ('.' != $file->getFilename() && '..' != $file->getFilename()) {
if ($file->isDir()) {
$files[$file->getFilename()] = recursive_list($file->getPathname());
} else {
$files[] = $file->getFilename();
}
}
}
return $files;
}
print_r(recursive_list($path));
?>

Above are few different methods to get the files and directories recursively using the PHP script, there are other different ways by which you can do the same things.

Support Me by Sharing This Article

Ankur Kumar Singh

I am a PHP programmer having some knowledge about Linux. I am always interested in web development and knowledge sharing. I am full time tech evangelist part time human being. :-)

Leave a comment/Ask Question

shares