The glob() Function In PHP

25th March 2008

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.

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

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.

  1. $globOut = glob('debug/*.html');
  2. if(count($globOut)>0){
  3. $globOut = array_reverse($globOut);
  4. echo '<table;><tr><th>File</th><th>Size</th><th>Timestamp</th></tr>';
  5. foreach($globOut as $filename){
  6. 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>';
  7. }
  8. echo '</table>';
  9. }else{
  10. echo '<p>No debug files found.</p>';
  11. }

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.