How to Get All Files and Folders in a Folder with PHP

PHP

In this tutorial, I will show you how to get all the files, subfolders and files in subfolders within a folder recursively using PHP, with the help of a detailed example.

If you are building a website or web application with PHP, there may be times where you need to get and make a list of all the files and subfolders in a folder. For example, an image application where you need to display all the subfolders and images inside those subfolders of a user.

I talked about how to get all files in a folder and how to get all subfolders in a folder in our other PHP tutorials. I will now combine the methods we used in these two tutorials to get everything, all files and subfolders, in a main folder using PHP.

Before we start, we will prepare the following test environment with folders and files like below:

/images/
/images/city/
/images/city/city1.jpg
/images/nature/
/images/nature/nature1.jpg
/images/nature/nature2.jpg
/images/people/
/images/people/people1.jpg
/images/people/family/family1.jpg
/images/people/family/family2.jpg
/images/people/family/family3.jpg

Note the use of multiple levels of subfolders within subfolders of the main images folder. If we say city, nature and people subfolders are level 1 depth, the family subfolder in the people subfolder is level 2 depth. Your folder setup can be a lot more complicated.

Now, let's start with our PHP script.

Get All Files and Subfolders in a Folder

In our example above, we know the folder structure and how many levels of subfolders there are. However, we should build our script to account for all cases, i.e. an indefinite number of files and subfolders and subfolder levels. Since we can't achieve an indefinite levels of depth with a basic loop, we will need to use a recursive function like the following:

function get_all($dir, &$files = array()) {
  if ($tmp = opendir($dir)) {
    while (($file = readdir($tmp)) !== false) {
      $path = $dir.'/'.$file;
      
      if (!is_dir($path)) {
        $files[] = $path;
      } else if ($file != '.' && $file != '..') {
        get_all($path, $files);
        $files[] = $path;
      }
    }
  }

  return $files;
}

We defined our function get_all() with two parameters; the first one is the current directory and the second one is an array passed by reference to collect all the files and subfolders in it. We start with checking and opening the directory via opendir() and continue with our while loop to loop through all the files and folders in the current directory. We assign the path of the current file to a $path variable for ease of notation. Then, we check if the current file is a directory or not. If it is not a directory, we add it to our $files array. If it is a directory (subfolder), we apply the same function to it and then add it to the $files array.

This function will check all files and subfolders recursively and return their info with $files array. You can then process this array as you wish, e.g. display all the files and subfolders using:

function get_all($dir, &$files = array()) {
  if ($tmp = opendir($dir)) {
    while (($file = readdir($tmp)) !== false) {
      $path = $dir.'/'.$file;
      
      if (!is_dir($path)) {
        $files[] = $path;
      } else if ($file != '.' && $file != '..') {
        get_all($path, $files);
        $files[] = $path;
      }
    }
  }

  return $files;
}

$all_files = get_all('images');

foreach ($all_files as $file) {
  echo $file.'<br>';
}

The above code will display the following list:

images/city/city1.jpg
images/city
images/nature/nature1.jpg
images/nature/nature2.jpg
images/nature
images/people/family/family1.jpg
images/people/family/family2.jpg
images/people/family/family3.jpg
images/people/family
images/people/people1.jpg
images/people

You can then filter or sort the file list as you wish or use them in other processes in your application.

I tried to keep things as simple as possible in this tutorial but there are other ways for getting all the files and folders in a folder with PHP. For example, you can also use the DirectoryIterator class in a similar sense.

f t g+ in