Interacting with directories in PHP

We have looked extensively at manipulating files on the file system. This time we will look at directories, the other major component of conventional file systems.

A directory can contain files and sub-directories. Conventional file systems (such as those used on Windows and UNIX/Linux platforms) also contain two special subdirectories: '.' representing the current directory; and '..' representing the parent directory. PHP provides several interfaces for listing the entries in a directory.

Python vs. PHP: Choosing your next project's language

Listing the contents of a directory

The most elementary directory interface in PHP closely resembles that of the C language - in which PHP itself is written. The following script creates an array of the entries in the current directory.

01 <?
02 $dir = opendir(".");
03 $ents = array();
04 while(($ent = readdir($dir)) !== false) {
05 $ents[] = $ent;
06 }
07 closedir($dir);
08 ?>

On line 02, this script creates a directory 'handle'. This is just a reference to a given directory, which is passed to other directory functions. Line 03 creates an array, $ents, in which to store directory entries.

At line 04, the script iterates through all entries in the directory. It uses the !== operator to test when the loop terminates. This is not the same as the != or 'not equal to' operator. The !== operator evaluates to true when the arguments are not equal and they are of the same type. This is important, because readdir() may return a file called 'false'.

While the loop condition is true, line 05 pushes the filename $ent, returned by readdir(), on to the array $ents. Finally, when the loop is terminated, the script closes the directory handle, $dir, using closedir().

The order in which the entries are returned is generally chronological, but that is not guaranteed.

In this script we only need to iterate through the directory entries once, but what if we needed to reuse the directory handle? Subsequent calls to readdir() would return false. As such, the handle needs to be 'rewound'. This can be achieved by calling rewinddir() after iterating through the handle via readdir().

Object orientation

A slightly more object-orientated version of this process of listing a directory's contents exists within PHP. Consider the following script:

01 <?
02 $dir = dir(".");
03 $ents = array();
04 while(($ent = $dir->read()) !== false) {
05 $ents[] = $ent;
06 }
07 $dir->close();
08 ?>

On line 02, the script called the dir() function with an argument of '.' - that is, the current directory. This function returns an object. On line 04, $dir->read() is called instead of readdir(). Both functions are equivalent. Likewise, $dir->close() is called instead of closedir().

The rewinddir() function is also bound to the $dir->rewind() method, allowing all of the functionality of the conventional interface to be accessed through this object.

As such, the benefit of using this interface over the previous one is that it is tightly integrated. Some programmers find it easier and more efficient to use; experiment with each and decide which you prefer.

Where am I?

Another important aspect of interacting with directories is to be able to determine the current directory and traverse a directory structure. The following script illustrates how simple this is to do in PHP.

01 <?
02 $cwd = getcwd();
03 echo "Current working directory: $cwd\n";
04 if(chdir("./tmp")) {
05 $cwd = getcwd();
06 echo "Current working directory: $cwd\n";
07 } else {
08 exit("No such directory: tmp\n");
09 }
10 ?>

This script reports the current directory to the user and then attempts to change the directory to './tmp' - that is, change to the sub-directory 'tmp' in the initial current working directory.

To get the current directory, line 02 calls getcwd() which returns the directory as a string. This is then reported to the user on line 03. On line 04, the script attempts to change directory to './tmp' using the chdir() - change directory - function. This function returns true if successful and false on error. If successful, the script returns the new working directory; otherwise, it exits, notifying the user that the directory does not exist.

Users looking to increase their PHP skills should attempt to integrate the scripts covered in this tutorial and to detect the special files '.' and '..' when listing directory entries.

Join the newsletter!

Error: Please check your email address.
Rocket to Success - Your 10 Tips for Smarter ERP System Selection
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

Ben Ramsden

Sharp PN-40TC1 Huddle Board

Brainstorming, innovation, problem solving, and negotiation have all become much more productive and valuable if people can easily collaborate in real time with minimal friction.

Sarah Ieroianni

Brother QL-820NWB Professional Label Printer

The print quality also does not disappoint, it’s clear, bold, doesn’t smudge and the text is perfectly sized.

Ratchada Dunn

Sharp PN-40TC1 Huddle Board

The Huddle Board’s built in program; Sharp Touch Viewing software allows us to easily manipulate and edit our documents (jpegs and PDFs) all at the same time on the dashboard.

George Khoury

Sharp PN-40TC1 Huddle Board

The biggest perks for me would be that it comes with easy to use and comprehensive programs that make the collaboration process a whole lot more intuitive and organic

David Coyle

Brother PocketJet PJ-773 A4 Portable Thermal Printer

I rate the printer as a 5 out of 5 stars as it has been able to fit seamlessly into my busy and mobile lifestyle.

Kurt Hegetschweiler

Brother PocketJet PJ-773 A4 Portable Thermal Printer

It’s perfect for mobile workers. Just take it out — it’s small enough to sit anywhere — turn it on, load a sheet of paper, and start printing.

Featured Content

Product Launch Showcase

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?