Loop Through Folders with glob() of PHP

There are some times when we need to get entire list of objects (files and directory) in an specific director. In that case We need to loop through that directory to implement our necessary action. Basically I am used to include php helper files to framework. This would require a relatively large snippet of code if we try to use open_dir(); however, with glob(), it’s easier to me. Lets check the below code:

$myDir = "/uploaded/temp/"; //Define the example directory
    foreach(glob($myDir.'*') as $file)  
    {  
        echo "File Name: ".$file."
"; }

Above script will print all the file name & file type of the “/uploaded/temp/” directory.

File Name: /uploaded/temp/.htaccess
	File Name: /uploaded/temp/test1.jpg
	File Name: /uploaded/temp/test2.gif
	File Name: /uploaded/temp/test3.png
	File Name: /uploaded/temp/thumb_test1.jpg
	File Name: /uploaded/temp/thumb_test2.gif
	File Name: /uploaded/temp/thumb_test3.png
	File Name: /uploaded/temp/sub_folder

So, what is doing by a glob() function. glob() function used to searches for all the pathnames matching pattern according to the rules used by the libc glob() function, and the rules are similar to the rules used by common shells. This method has been available since PHP 4.3.

glob() function used to get two parameter with the second argument being optional. The first argument is the string path pattern.

We can limit the search to specific filetypes or even multiple directories at the same time by using multiple asterixes “*”. For example:

$myDir = "uploaded/*/temp/*";
	foreach(glob($myDir) as $file)  
	{  
	    echo "File Name:" . $file . "
"; }

This will search “uploaded/any/temp/any” and will return a list of the files that match the pattern. We can even take things a step further, and be more specific by including a file format in our foreach statement. See below example:

$myDir = "uploaded/*/temp/{*.jpg,*.gif}";
	foreach(glob($myDir, GLOB_BRACE) as $file)  
	{  
		echo "File Name: " . $file . "
"; }

Above script will output almost below:

File Name: /uploaded/temp/test1.jpg
	File Name: /uploaded/temp/test2.gif
	File Name: /uploaded/temp/thumb_test1.jpg
	File Name: /uploaded/temp/thumb_test2.gif

As I mentioned previously, the second parameter provides a very nice set of optional flags. These allow us to change the way of the glob() function behaves. Valid flags are:

  • GLOB_MARK – Used to adds a slash to each directory returned
  • GLOB_NOSORT – Used to return files as they appear in the directory (no sorting)
  • GLOB_NOCHECK – Used to return the search pattern if no files matching it were found
  • GLOB_NOESCAPE – Backslashes do not quote metacharacters
  • GLOB_BRACE – Used to expands {a,b,c} to match ‘a’, ‘b’, or ‘c’
  • GLOB_ONLYDIR – Used to return only directory entries which match the pattern
  • GLOB_ERR – Used to stop on read errors (like unreadable directories), by default errors are ignored.

One example of these flags is that if you wish to only print subfolder names, we could use GLOB_ONLYDIR. For example:

$myDir = "uploaded/*/temp/*";
	foreach(glob($myDir, GLOB_ONLYDIR) as $folder)  
	{  
		echo "Folder Name: " . $folder . "
"; }

Above script will output almost below:

Folder Name: /uploaded/temp/sub_folder

Author Info

Shah Alom

Hi, This is Mohammad Shah Alom, My passion is Programming & Web Development. I am Founder of Micro Solutions Bangladesh. My Facebook profile shahalom1983 & Twitter Profile shahalom_83