PHP glob() | Get List of all Files/Directories

The PHP glob() function is used when we need to get the list of files (based on the pattern given to it) in the form of arrays. For example:

<?php
   $files = glob("*.*");
   print_r($files);
?>

The output produced by above PHP example on glob() function is:

php glob function

And here is the snapshot of the current directory, where all these files are available:

php glob function example

In above example, consider the following code:

glob("*.*")

The * is used in pattern to get all. That is, the first * refers to the name, and the second * refers to extension. Therefore if we decode the code, then it will be like glob("AllFiles.AllExtensions"), this is not the code, but I've written this for you to get the scenario. So, the glob("*.*") is used to get all files.

Note - If you want to get only .txt files, then use glob("*.txt"). If you want to list only .php files, then use glob("*.php")

The same program can also be created in this way, that prints the list of all files available in current directory, one by one:

<?php
   $files = glob("*.*");
   echo "<p>List of all files:</p>";
   for($i=0; $i<count($files); $i++)
   {
      echo $files[$i];
      echo "<br>";
   }
?>

Now the output of above PHP program should be:

php list all files using glob

PHP glob() Syntax

The syntax of glob() function in PHP, is:

glob(pattern, flags)

The first (pattern) parameter is required, whereas the last (flags) parameter is optional.

Note - The pattern parameter is used to define the pattern to search for the files/directories

Note - The flags parameter is used when we need to apply some extra features to search for the files/directories, using any of the following:

  • GLOB_BRACE - Used to expand {a, b, c} to match 'a', 'b', or 'c'
  • GLOB_ERR - Used to stop errors
  • GLOB_MARK - Used to insert a slash to each returned directory/folder
  • GLOB_NOCHECK - Used to return the search pattern, in case if no match were found
  • GLOB_NOSORT - Used to return files as they are in the directory without sorting
  • GLOB_NOESCAPE - Backslashes do not quote metacharacters in this case
  • GLOB_ONLYDIR - Used when we need to get the list of directories only

PHP glob() - List all Files/Folders

<?php
   $files = glob("*");
   echo "<p>List of all Files and Folders:</p>";
   for($i=0; $i<count($files); $i++)
   {
      echo $files[$i];
      echo "<br>";
   }
?>

The output produced by above PHP example, is:

php glob list all files folders

PHP glob() - Add Slash to Each Returned Directory/Folder

<?php
   $files = glob("*", GLOB_MARK);
   echo "<p>List of all Files and Folders:</p>";
   for($i=0; $i<count($files); $i++)
   {
      echo $files[$i];
      echo "<br>";
   }
?>

The output should be:

php glob add slash after each returned directory

Notice the slash after dashboard, img, webalizer, and xampp. These are the directory/folder.

PHP glob - Return Search Pattern itself if No Match Found

<?php
   $files = glob("*.dat", GLOB_NOCHECK);
   for($i=0; $i<count($files); $i++)
   {
      echo $files[$i];
      echo "<br>";
   }
?>

The output produced by above example should be *.dat

PHP glob() - List Directories Only

<?php
   $files = glob("*", GLOB_ONLYDIR);
   echo "<p>List of all Directories:</p>";
   for($i=0; $i<count($files); $i++)
   {
      echo $files[$i];
      echo "<br>";
   }
?>

The output of above PHP example is given in the following snapshot:

php glob list directories only

PHP Online Test


« Previous Tutorial Next Tutorial »

Follow/Like Us on Facebook




Subscribe Us on YouTube