PHP glob(): Get List of all Files and 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 the above PHP example on the 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 the above example, consider the following code:

glob("*.*")

The * is used in the pattern to get all. That is, the first * refers to the name, and the second * refers to the extension. Therefore, if we decode the code, then it will be like glob("AllFiles.AllExtensions"). This is not the code, but I've written it 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, which prints the list of all files available in the 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 the above PHP program should be:

php list all files using glob

PHP glob() Syntax

The syntax of the 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 or directories.

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

PHP glob(): List all files and 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 the above PHP example is:

php glob list all files folders

PHP glob(): Add a slash to each returned directory or 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 directories or folders.

PHP glob(): Return the search pattern itself if not match is found

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

The output produced by the 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 the above PHP example is given in the following snapshot:

php glob list directories only

PHP Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!