How to Get All Files in a Folder with PHP

PHP

In this quick tutorial, I will show you how to get all the files in a folder with PHP and store that information in an array that you can then use as you wish.

For a number of reasons, you may need to get and either display or process all the files that are stored in a folder (directory) on your website. For example, you may want to display all the images in a folder on your photo gallery or display a link to all the PDF files in a folder on your download page or update/delete all the files in a directory during an application update.

PHP makes it possible to get information about the files and folders on your website using the readdir() and glob() functions. We will see how we can get the files in a folder and store their names in an array using both functions below.

The following examples assume that there are only files and no subfolders in our folder. I will demonstrate the case where you have files and subfolders in a folder on another tutorial.

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

Let's say you have your images stored in the /images/ folder which is located in the root directory of your website. If you create an images.php file in the root directory, you can use the following code to get all the images in the images folder:

<?php
$images = glob('images/*');
?>

TIP: In PHP, we don't add a / to the beginning of the resource paths if they are in the same directory as the PHP script referring to that resources. In this case, we used images/, not /images/.

What the above code does is that it checks the images folder and gets all the files (and folders) inside it using the wildcard character (*), and stores that information in the $images array. It is up to you what to do with the $images array. For example, if you want to display all the image path names on your images.php page, you can use the following foreach loop in your code:

<?php
$images = glob('images/*');

foreach ($images as $image) {
  echo $image.'<br>';
}
?>

This will display a list of image path names like the following:

images/image1.jpg
images/image2.jpg
images/image3.jpg

If you want to display only the image names, you can use str_replace() function to get rid of images/ in the path names.

<?php
$images = glob('images/*');

foreach ($images as $image) {
  echo str_replace('images/', '', $image).'<br>';
}
?>

If you want to display the images themselves, you can modify the output HTML code as follows:

<?php
$images = glob('images/*');

foreach ($images as $image) {
  echo '<img src="'.$image.'"><br>';
}
?>

TIP: Pay attention to the use of single and double quotes in your code, as the wrong use will cause errors which may not always be visible on the displayed page but may be visible to browsers.

Once you get the files stored in an array, you can do pretty much anything you want with them (within PHP limits); these are just basic examples to give you an idea how to get all the files in a folder.

Now, let's continue with the next method.

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

Another function we can use to get the files in a folder is readdir(), which reads the files (and folders) in a folder. It's not like opening and reading the files, it's rather getting information about the files. Before using readdir(), we need to open the directory with opendir() where readdir() will read from.

<?php
$dir = 'images/';

if ($tmp = opendir($dir)) {

}
?>

What we did above is that we specified the images folder as our directory using the $dir variable and then opened the directory so that readdir() can start reading. $tmp is just a temporary variable that we will pass to readdir(). If the specified directory can't be opened, it will give an error.

We continue with looping through all the files inside the images folder as follows:

<?php
$dir = 'images/';

if ($tmp = opendir($dir)) {
  while (($image = readdir($tmp)) !== false) {

  }
}
?>

The while loop will continue looping till the last file is checked. We can output the file information at this stage like the following:

<?php
$dir = 'images/';

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

However, this will add . and .. to the beginning of the image list, which represent the images folder and its parent folder respectively. The output will be like the following:

.
..
image1.jpg
image2.jpg
image3.jpg

To remove the . and .., we can use an IF check:

<?php
$dir = 'images/';

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

This will get rid of the dots and only the file names will be displayed. Note that the file names do not include the path names when we use readdir(), because it reads the file names, not their paths like glob().

As in the previous example with glob(), you can list or display the images or do any other operation that you would like to do.

These are the two methods that you can use to list all the files in a directory with PHP. The second method with readdir() seems to be more appropriate since it directly reads the file names, but you can use both methods equally well as you wish.

f t g+ in