Tuesday, August 31, 2021

How to List Files In a Directory in PHP

In this article, we’ll discuss how you can get a list of all the files in a directory in PHP.

In your day-to-day PHP development, you often need to deal with a file system—for example, getting a list of files in a specific directory. PHP provides a few different ways that you can use to read directory contents easily. Today, we’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 discuss a family of opendir, readdir and closedir functions to see how you can use it to get a list of files in a specific directory.

The opendir function allows you to open up a directory handle, which you can use along with other functions for different operations on the directory. You need to pass a the path to the directory in the first argument of the opendir function. If the directory path is valid, a directory handle resource will be returned.

The readdir function allows you to read a directory. You need to provide a valid directory handle in the first argument of the readdir function, and you can iterate over all the entries and get a list of all files in a directory.

The closedir function allows you to close the directory handle which is opened by the opendir function. It’s a good practice to use the closedir function once you’ve done with your operations on the directory handle (which was initially opened by the opendir function).

Now, let’s see it in action as shown in the following example.

Firstly, we’ve used the opendir function to get the directory handle. Next, we’ve used the readdir function to iterate over the $handle directory handle and store a list of files in the $arrFiles array.

The scandir Function

In this section, we’ll see how you can use the scandir function to get directory contents.

The scandir function is a pretty straightforward way to get a list of files and directories in the specific directory. You just need to pass a directory path which you want to read in the first argument of the scandir function.

Let’s go through the following example to understand how it works.

As you can see, this provides a list of files and directories in a single call! If you just need to get the list of files in a directory, this is a better choice than using opendir, readdir and closedir.

The glob Function

The glob function 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 the specific directory.

As you can see, we’ve passed the * pattern since we want to read all contents. On the other hand, if you want to list only specific type of files, you can do that as well as shown in the following snippet.

in this case, $arrFiles would contain 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 can use the dir function, which is an object-oriented mechanism for reading a directory.

When you use the dir function, and pass a directory path in the first argument, it returns an instance of the Directory class, which you can use subsequently to read directory contents.

Let’s go through the following example to see how it works exactly.

First, we used the dir function to initialize an instance of the Directory class 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 section, we’ll see how you can use the FilesystemIterator class to read directory contents. The benefit of using the FilesystemIterator class is that you can extend the methods of this class and it also provides a lot of other useful methods that allow you to fetch various information about the files as well.

Let’s go through the following example to understand how it works.

As you can see, it’s really easy to use the FilesystemIterator class and iterate over the entries. In the above example, we’ve used the getFileName method of the FilesystemIterator class to get the file name. However, the FilesystemIterator class also provides several utility methods like getExtension, getSize, getPathname and so on.

So that’s how you can use the FilesystemIterator class to get a list of files in a directory.

Conclusion

Today, we discussed different ways that you can use to list all the files in a directory in PHP. Here's a table to compare each method to help you choose which one is right for you.

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

Survey: One-Third of People Who Quit Their Jobs Plan to Start Businesses

People are leaving their jobs at record rates. For a remarkable number of them, entrepreneurship could be the next step.

Black Girls Code's Founder on the Power of Trusting Your Intuition

Kimberly Bryant says listening to her gut, despite what others advised her, was one of the best moves she ever made for her company.

How to Use Presets and Filters in Lightroom Mobile

Admit it, applying presets and filters to an image you love is pretty darn fun. With just a tap of the screen, your image transforms before your eyes. Effects that would take hours to accomplish in the wet darkroom—if they could be accomplished at all—are achieved instantaneously, and errors or oversights made at the time of capture can be eradicated or improved immediately. Adobe Lightroom Mobile provides presets and filters all from the convenience of your mobile phone.

In this tutorial, you’ll learn the quick and easy steps it takes to apply presets and filters in Lightroom Mobile. Check out the options Lightroom Mobile provides, and read the steps that follow. You’ll learn how to put the finishing touches on an image so that you can achieve the exact aesthetic you are looking for.

How to Use Presets and Filters on Raw Photos in Lightroom Mobile

The Basics

Lightroom Mobile is a free app that lets you create, edit, and share high-quality images. Embedded Adobe Photoshop technology provides you with over 30 presets, allowing you to experiment with color, clarity, vignetting, and more—all for free. Upgrading to a Creative Cloud Photography plan gives you access to a few more editing options, as well as syncing across devices, a customizable website, Photoshop, and cloud storage.

Creative Cloud plans and pricing for individuals vary from $9.99/month to $74.49/month (or you can pay annually in one lump sum) and usually come packaged with other perks, apps, and GBs of cloud storage. (Businesses, students & teachers, and schools & universities are offered other plans and pricing.) You can also try it out by activating a free seven-day trial.

1. Open the Adobe Lightroom Mobile App in Loupe Mode

First step, open up Loupe mode by tapping the picture/plus icon in the Library.

2. Select Your Image

Choose a nice, juicy, high-resolution DNG (RAW) image so that you have the ultimate editing latitude.

3. Tap Presets in the Edit Panel

Now select Presets from the Edit panel.

There are two tabs of presets: Premium and Library. The Premium tab includes pre-loaded Portraits, Cinematic, Futuristic, Vintage, and Travel presets.

Library includes Color, Creative, B&W, Portraits, Defaults, Curve, Grain, Optics, and Sharpening.

4. Experiment

It’s super fun! This Vintage preset, VN07, gave a soft aesthetic reminiscent of sepia tone. To apply a preset to an image, tap the Checkmark icon.

5. Compare

Now let's see how the image looked before you applied the preset. Tap and hold on the image. Before at the top of the screen tells you it’s the original version. To go back to the preset version, let go of your tap-and-hold.

To reverse a preset you have applied, tap the Undo icon. 

To move forward with a preset, tap the Redo icon.

6. Add More Presets

To apply more than one preset on an image, tap the checkmark icon after each preset. Stacking multiple presets let's you quickly create a stylized photo.

We liked this B&W Punch preset, but wanted to add heavy grain, too.

After selecting Heavy Grain, we tapped the checkmark icon again.

Lightroom Mobile also gives you the ability to create and save your own specific presets. Stay tuned here for a follow-up tutorial on the details!

7. Reset Some or All Adjustments as Needed

To reset the image and remove the presets, tap Reset.

Then tap what you would like to reset.

8. Manage Your Presets.

Tap on the three-dots icon in the upper right.

Then tap Manage Presets.

Select the preset group you want to manage, Premium or Library.

And then toggle on or off the presets you would like to make visible or invisible. Once you know the presets you prefer, this can help save you from having to scroll a bunch to find them.

Have Fun With Presets

Capturing an image is just one part of the process. What you would like that image to portray through its aesthetics is another. You have so many options with presets. Have fun experimenting with the different ways an image can look—and make viewers feel!

Keep Learning About Lightroom and Photography

Here are a few more free tutorials and resources to help you study up on Lightroom and smartphone photography: