How To List Files & Folders In PHP – Quick Examples

Welcome to a quick tutorial on how to list files and folders in PHP. Want to get the contents of a folder in PHP?

The common ways to list files and folders in PHP are:

  1. Use scandir() to get an array of files and folders – $contents = scandir("FOLDER");
  2. Use glob() to selectively get files and folders.
    • $pictures = glob("FOLDER/*.{jpg,jpeg,png,gif}", GLOB_BRACE);
    • $folders = glob("FOLDER/*", GLOB_ONLYDIR);
  3. Lastly, use opendir() to loop through a folder.
    • $fh = opendir("FOLDER");
    • while (($entry = readdir($fh)) !== false) { echo $entry; }
    • fclose($fh);

That should cover the basics, but read on for more examples!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

REAL QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes List Files Folders Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

LIST FILES & FOLDERS

All right, let us now get into the various examples on how to list files and folders in PHP.

 

1) SCANDIR

1-scandir.php
<?php
// (A) GET ALL FILES + FOLDERS
$contents = scandir(__DIR__);

// PASS IN A SECOND PARAMETER TO SORT
//$contents = scandir($target, SCANDIR_SORT_DESCENDING);
//$contents = scandir($target, SCANDIR_SORT_NONE);

// (B) LOOP THROUGH ALL FILES + FOLDERS
foreach ($contents as $c) {
  if ($c!="." && $c!="..") {
    echo is_dir($c) ? "DIR" : "FILE" ;
    echo " - $c<br>";
  }
}

This is as easy as it gets, just use scandir("FOLDER") to get an array of all the files and folders. But take note, we can’t really “customize” this function to get “certain file types only”, and it is potentially bad for folders with a lot of files.

 

2) GLOB

2-glob.php
<?php
// (A) GET ALL PHP + TEXT FILES
$scripts = glob(__DIR__ . DIRECTORY_SEPARATOR . "*.{php,txt}", GLOB_BRACE);
print_r($scripts);

// (B) GET FOLDERS ONLY
$folders = glob(__DIR__ . DIRECTORY_SEPARATOR . "*" , GLOB_ONLYDIR);
print_r($folders);

To get a “customized files list”, use the glob() function instead – We can pretty much restrict to any file type, get folders only, and even do very specific searches.

 

 

3) OPENDIR

3-opendir.php
<?php
// (A) OPEN FOLDER + READ ENTRIES
$target = __DIR__;
if ($fh = opendir($target)) {
  while (($entry = readdir($fh)) !== false) {
    // (B) IGNORE "." AND ".."
    if ($entry!="." && $entry!="..") {
      // (C) FILE OR FOLDER?
      $thisE = $target . DIRECTORY_SEPARATOR . $entry;
      echo is_dir($thisE) ? "Directory" : "File";
      echo " - $thisE<br>";
    }
  }
  closedir($fh);
}

Have a massive folder with thousands of files? This is how we should deal with it – Read the entries one-by-one.

 

4) RECURSIVE GET SUB-FOLDERS

4-recursive.php
<?php
// (A) RECURSIVE GET FILES + FOLDERS
function getFF ($path) {
  // (A1) GET FILES
  $files = glob("$path*");
 
  // (A2) GET FOLDERS
  $folders = glob("$path*", GLOB_ONLYDIR);
 
  // (A3) OUTPUT CURRENT PATH
  echo "<h1>$path</h1><br>";
  print_r($files);
  print_r($folders);

  // (A4) RECURSIVE LOOP
  if (count($folders)!=0) {
    foreach ($folders as $f) {
      getFF($f . DIRECTORY_SEPARATOR);
    }
  }
}
 
// (B) GO!
getFF(__DIR__ . DIRECTORY_SEPARATOR);

Lastly, getting the sub-folders are kind of tricky – We need a recursive function that will automatically drill into the sub-folders.

 

 

USEFUL BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

INFOGRAPHIC CHEAT SHEET

How To List Files Folders In PHP (Click To Enlarge)

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *