In this article I’ll talk about a common task you might have experienced while developing a PHP application: listing files and directories. I’ll discuss several basic and advanced solutions, each having its pros and cons. First I’ll present three approaches that use some very basic PHP functions and then progress to more robust ones which make use of SPL Iterators. Show
For the purposes of the discussion, let’s assume a directory structure that looks like the one below: \---manager | \---user | \---document.txt | \---data.dat | \---style.css |---article.txt |---master.dat |---script.php |---test.dat |---text.txt The Basic SolutionsThe first set of approaches demonstrate the use of the functions Using glob()The first function to discuss is
Let’s see some examples! To search in the directory for all files and directories that end with .txt, you would write: <?php $filelist = glob("*.txt"); If you display array ( 0 => 'article.txt', 1 => 'text.txt' ) If you want a list of files and directories that begin with “te”, the code to write is: <?php $filelist = glob("te*"); The output is: array ( 0 => 'text.txt' ) To get a list of directories only which contain “ma”, the code is: <?php $filelist = glob("*ma*", GLOB_ONLYDIR); In this last example, the output is: array ( 0 => 'manager' ) Notice that the last example makes use of the Using opendir() and readdir()The second
approach to read files and directories I’d like to discuss involves the functions
Unlike To parallel with the <?php $filelist = array(); if ($handle = opendir(".")) { while ($entry = readdir($handle)) { if (strpos($entry, "te") === 0) { $filelist[] = $entry; } } closedir($handle); } The output is the same as the previous example. But if you execute the code above and output the value of The second example shows how to retrieve only the files contained in a given path. <?php $filelist = array(); if ($handle = opendir(".")) { while ($entry = readdir($handle)) { if (is_file($entry)) { $filelist[] = $entry; } } closedir($handle); } As you might guess, using the above code produces the following output: array ( 0 => 'article.txt', 1 => 'master.dat', 2 => 'script.php', 3 => 'test.dat', 4 => 'text.txt' ) Using scandir()And finally, I’d like to present the This example shows how to retrieve files and directories which start with the string “te”: <?php $entries = scandir("."); $filelist = array(); foreach($entries as $entry) { if (strpos($entry, "te") === 0) { $filelist[] = $entry; } } Let’s use the SPL IteratorsNow let’s talk about some SPL Iterators. But before going into deep about their use, let me introduce them and the SPL library. The SPL provides a series of classes for object-oriented data structures, iterators, file handlers, and other features. One of the pros is that Iterators are classes and so you can extend them to better fit your needs. Another
advantage is that they have native methods that are really helpful in achieving many of the common task you might face and you have them in just one place. Take as an example the use of Of course, PHP can provide you the
same information using functions like As said in the introduction, I’ll show the use of
What actually differs in these iterators is the approach they use to navigate the given path. The FilesystemIteratorUsing the <?php $iterator = new FilesystemIterator("."); $filelist = array(); foreach($iterator as $entry) { if (strpos($entry->getFilename(), "te") === 0) { $filelist[] = $entry->getFilename(); } } With the code above, the result is the same of the previous examples. The second example that uses the <?php $iterator = new FilesystemIterator("."); $filter = new RegexIterator($iterator, '/t\.(php|dat)$/'); $filelist = array(); foreach($filter as $entry) { $filelist[] = $entry->getFilename(); } In this case the output is: array ( 0 => 'script.php', 1 => 'test.dat' ) The RecursiveDirectoryIteratorThe <?php $iterator = new RecursiveDirectoryIterator('.'); $filter = new RegexIterator($iterator->getChildren(), '/t\.(php|dat)$/'); $filelist = array(); foreach($filter as $entry) { $filelist[] = $entry->getFilename(); } The GlobIteratorThe <?php $iterator = new GlobIterator("te*"); $filelist = array(); foreach($iterator as $entry) { $filelist[] = $entry->getFilename(); } ConclusionsIn this article I’ve illustrated different ways to achieve the same goal: how to retrieve and filter files and directories in a given path. These are some key points to remember:
Do you know of other approaches to achieve the goal? If so and you want to share with us, go ahead. Knowledge sharing is always welcome. Image via Fotolia How to get directory file list in PHP?The scandir() function in PHP is an inbuilt function that is used to return an array of files and directories of the specified directory. The scandir() function lists the files and directories which are present inside a specified path.
What is glob() in PHP?The glob() function returns an array of filenames or directories matching a specified pattern. The glob() function returns. An array containing the matched files/directories, Returns an empty array if no file is matched, FALSE on error.
How can I get a list of all the subfolders and files present in a directory using PHP?PHP using scandir() to find folders in a directory
To check if a folder or a file is in use, the function is_dir() or is_file() can be used. The scandir function is an inbuilt function that returns an array of files and directories of a specific directory.
What is __ DIR __ in PHP?The __DIR__ can be used to obtain the current code working directory. It has been introduced in PHP beginning from version 5.3. It is similar to using dirname(__FILE__). Usually, it is used to include other files that is present in an included file.
|