How to Get the Subfolders in a Folder with PHP

PHP

When you are working on a website with many folders and subfolders, it may be handy to know how to access the subfolders within another folder. In this detailed tutorial, I will demonstrate how to get all the subfolders in a folder using a PHP script.

We talked about how to get all files in a folder and how to get the file count in a folder with PHP in other tutorials, we will now continue with getting the subfolders in a folder with PHP.

Some of the files that are used on websites and web applications such as images, videos, text and PDF files etc. are usually organized by their date or category, and they are stored in subfolders (subdirectories). If you have a website or application with many files in many folders, there may be times where you may need to access and get information about all the subfolders in some main folders. A simple PHP script will be very handy in such a case.

We will be using the glob() and readdir() functions to demonstrate how to get and list the subfolders within another folder. Our first example will focus on a folder with only one level depth of subfolders, i.e. no subfolders within subfolders.

How to Get All Subfolders in a Folder with PHP Using glob()

Assume that you have a main videos folder named as videos and it is located in your root directory as such /videos/. In this folder, you have many subfolders that group the videos based on their categories like the following:

videos
- cars
- education
- finance
- funny
- games
- health
- life
- news
- technology

To get the subfolders of the videos folder, you will need to put the following code in your PHP script:

<?php
$subfolders = glob('videos/*', GLOB_ONLYDIR);

foreach ($subfolders as $subfolder) {
  echo $subfolder.'<br>';
}
?>

We used the GLOB_ONLYDIR flag to instruct the glob() function to return only the directories (subfolders) in the videos folder. The above code will return the following list of folder paths:

videos/cars
videos/education
videos/finance
videos/funny
videos/games
videos/health
videos/life
videos/news
videos/technology

If you need only the names of the subfolders, you can remove "videos/" from the folder paths with the help of str_replace() function like the following:

<?php
$subfolders = glob('videos/*', GLOB_ONLYDIR);

foreach ($subfolders as $subfolder) {
  echo str_replace('videos/', '', $subfolder).'<br>';
}
?>

What you do with the subfolder list once you store it in an array ($subfolders) is up to you. You can list them as we did above or do additional processes as your application requires.

We can continue with the second method now.

How to Get All Subfolders in a Folder with PHP Using readdir()

Another useful function of PHP while working with files and folders is readdir(). When using readdir(), we first open the target folder/directory and then start a loop that will check each file and folder within that target folder. The following code will list all the subfolders in the videos folder.

<?php
$dir = 'videos/';

if ($tmp = opendir($dir)) {
  while (($video = readdir($tmp)) !== false) {
    if ($video != '.' && $video != '..' && is_dir($dir.'/'.$video)) {
      echo $video.'<br>';
    }
  }
}
?>

We removed the current folder and parent folder entries, (.) and (..) respectively, and also filtered out files using is_dir() function within our IF statement.

If you want to store the list of subfolders in an array which you can use for any purpose later on, you can use the following code in your script.

<?php
$dir = 'videos/';
$subfolders = array();

if ($tmp = opendir($dir)) {
  while (($video = readdir($tmp)) !== false) {
    if ($video != '.' && $video != '..' && is_dir($dir.'/'.$video)) {
      array_push($subfolders, $video);
    }
  }
}
?>

We declared an empty array ($subfolders) and then populated this array using the array_push() function. You can now use this array of subfolders as you wish in your script.

f t g+ in