What Ways Besides Opendir Can a Program Read a Directory

Read Time: 5 mins Languages:

In this article, nosotros'll talk over how you tin can get a list of all the files in a directory in PHP.

In your day-to-twenty-four hours PHP development, you ofttimes demand to deal with a file organisation—for instance, getting a list of files in a specific directory. PHP provides a few unlike ways of reading the contents of a directory easily. Today, nosotros'll go through all these methods, along with examples to understand how each one works.

The opendir, readdir, and closedir Functions

In this section, we'll talk over the opendir, readdir, and closedir functions to see how you can employ them to get a listing of files in a specific directory.

The opendir part allows yous to open up upwards a directory handle, which you tin can employ along with other functions for different operations on the directory. You demand to pass the path to the directory in the offset argument of the opendir function. If the directory path is valid, a directory handle resource will be returned.

The readdir function allows yous to read a directory. You need to provide a valid directory handle in the showtime statement of the readdir function, and y'all can iterate over all the entries and go a list of all files in a directory.

The closedir part allows you to close the directory handle which is opened by the opendir office. It's a good do to apply the closedir function in one case you lot're done with your operations on the directory handle (which was initially opened by the opendir function).

Now, let's see information technology in action in the following instance.

Firstly, we've used the opendir role to get the directory handle. Next, we've used the readdir part to iterate over the $handle directory handle and shop a list of files in the $arrFiles assortment.

The scandir Function

In this section, we'll see how y'all can apply the scandir office to get directory contents.

The scandir function is a pretty straightforward mode to become a listing of files and directories in a specific directory. You just need to pass a directory path which yous want to read in the first argument of the scandir function.

Permit'due south go through the following example to sympathise how it works.

Equally you can meet, this provides a list of files and directories in a unmarried telephone call! If you but need to get a listing of files in a directory, this is a better pick than using opendir, readdir, and closedir.

The glob Function

The glob role works similarly to the scandir function, with the difference that it allows you to specify a pattern for filtering and matching the files in the directory.

Let's see how to use the glob function to read all the contents of a specific directory.

As you lot can come across, we've passed the * blueprint since we desire to read all the contents. On the other paw, if you want to listing only a specific type of file, you can do that as well, as shown in the following snippet.

in this case, $arrFiles would incorporate only the names of .txt files.

The dir Function

The options that we've discussed so far allow you to read directory contents in a procedural way. In this section, we'll see how you lot tin can employ the dir function, which is an object-oriented mechanism for reading a directory.

When you utilize the dir office, and pass a directory path in the outset argument, it returns an instance of the Directory form, which you can employ subsequently to read the directory contents.

Allow'south go through the following example to see how it works exactly.

First, we used the dir part to initialize an instance of the Directory form into the $objDir variable. Next, we used the read method of the Directory class to iterate over all the entries.

The FilesystemIterator Class

In this department, we'll encounter how you tin employ the FilesystemIterator form to read directory contents. The benefit of using the FilesystemIterator grade is that you can extend the methods of this class, and information technology also provides a lot of other useful methods that let you lot to fetch various data about the files.

Let's become through the following example to empathize how it works.

As you tin can run across, it's really easy to employ the FilesystemIterator class and iterate over the entries. In the above example, nosotros've used the getFileName method of the FilesystemIterator class to get the file name. Still, the FilesystemIterator class also provides several utility methods like getExtension, getSize, getPathname, and so on.

Then that's how yous tin can use the FilesystemIterator class to get a list of files in a directory.

Determination

Today, nosotros discussed different ways of listing all the files in a directory in PHP. Here's a tabular array to compare each method to help you choose which one is right for you.

Function or Class Notes
opendir, readdir and closedir functions non the simplest or almost feature-rich method
scandir function simplest way to read a file list
glob function very simple and allows pattern matching
dir function object-oriented version of readdir
FilesystemIterator course can provide other info near files, like size or permissions

tuckerwithand.blogspot.com

Source: https://code.tutsplus.com/tutorials/how-to-list-files-in-a-directory-in-php--cms-38044

0 Response to "What Ways Besides Opendir Can a Program Read a Directory"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel