Most of us uses the PHP’s opendir() function to open a directory and then loop through the directory for reading its content using readdir() function which requires a lot of repetitive code every time we search for a folder.
Today, I came to know about PHP’s glob() function which is much smarter than opendir().
Let’s take an example:- Suppose we want to display the images of a directory(images/) then instead of writing the number of lines to accomplish this using opendir() and readdir(), we can do it in 2-3 lines of code.
$dir = "images/*";
// Open a known directory, and proceed to read its contents
foreach(glob($dir) as $file)
echo '<img src='.$file.' height=50 width=50 />';