The glob() Function In PHP

The glob() function in PHP uses simple pattern matching to find files and directories in a directory and return those file names as an array. It simplifies all of the PHP directory functions, so instead of opening the directory and then reading every file name one by one, you can just use glob() and so it in one function call. Additionally, glob() works very closely with the file system and so it very quick.

Here is a simple example.

$globOut = glob('*');

The $globOut variable now contains a list of all of the files and directories in the root folder. If you want to find all of the files ending with HTML in a directory then use the following.

The function takes another parameter which is a constant that defines some ways of changing the output. Here is a list of the available constants.

  • GLOB_MARK : Adds a slash to each item returned.
  • GLOB_NOSORT : Return files as they appear in the directory (no sorting).
  • GLOB_NOCHECK : Return the search pattern if no files matching it were found.
  • GLOB_NOESCAPE : Backslashes do not quote metacharacters.
  • GLOB_BRACE : Expands {a,b,c} to match 'a', 'b', or 'c'.
  • GLOB_ONLYDIR : Return only directory entries which match the pattern.
  • GLOB_ERR : Stop on read errors (like unreadable directories), by default errors are ignored.

If you wanted to find all of the files in the root folder that end in either .php or .html then you can use the GLOB_BRACE constant in the following way.

$globOut = glob('*.{php,html}',GLOB_BRACE);

In this example the string '*.{php,html}' is split into *.php and *.html and so the function matches both file types.

There are numerous different things that can be done with the function, for example, to print out a list of all of the HTML files in a directory as a list of links use the following little snippet.

$globOut = glob('*.html');
if(count($globOut)>0){ // make sure the glob array has something in it
 foreach ($globOut as $filename){
  echo '<a href="'.$filename.'" title="'.$filename.'">'.$filename.'</a><br />';
 }
}else{
 echo 'No files found.<br />';
}

In a recent application I had a directory of files that each had some debugging information in them that were created during important function calls. Each file was names with a timestamp and a .html to say when the file was created. I wanted to create a bit of code that would allow me to view the files easily as a table, with the most recently created file at the top.

$globOut = glob('debug/*.html');
if(count($globOut)>0){
 $globOut = array_reverse($globOut);
 echo '<table;><tr><th>File</th><th>Size</th><th>Timestamp</th></tr>';
 foreach($globOut as $filename){
  echo '<tr><td><a href="'.$filename.'" title="'.$filename.'">'.$filename.'</a></td><td>'.filesize($filename).'</td><td>'.date('l dS \of F Y h:i:s A',str_replace(array('debug/','.html'),array('',''),$filename)).'</td></tr>';
 }
 echo '</table>';
}else{
 echo '<p>No debug files found.</p>';
}

One thing to be careful of is that there are some compatibility issues with the glob() function on some systems, especially PHP 4 and earlier. So you might want to check that it works or at least create an alternative before relying on it.

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
14 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.