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 PC World newsletter!

Error: Please check your email address.

Struggling for Christmas presents this year? Check out our Christmas Gift Guide for some top tech suggestions and more.

Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World

Most Popular Reviews

Follow Us

Best Deals on GoodGearGuide

Shopping.com

Latest News Articles

Resources

GGG Evaluation Team

Kathy Cassidy

STYLISTIC Q702

First impression on unpacking the Q702 test unit was the solid feel and clean, minimalist styling.

Anthony Grifoni

STYLISTIC Q572

For work use, Microsoft Word and Excel programs pre-installed on the device are adequate for preparing short documents.

Steph Mundell

LIFEBOOK UH574

The Fujitsu LifeBook UH574 allowed for great mobility without being obnoxiously heavy or clunky. Its twelve hours of battery life did not disappoint.

Andrew Mitsi

STYLISTIC Q702

The screen was particularly good. It is bright and visible from most angles, however heat is an issue, particularly around the Windows button on the front, and on the back where the battery housing is located.

Simon Harriott

STYLISTIC Q702

My first impression after unboxing the Q702 is that it is a nice looking unit. Styling is somewhat minimalist but very effective. The tablet part, once detached, has a nice weight, and no buttons or switches are located in awkward or intrusive positions.

Latest Jobs

Shopping.com

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?