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.

Our Back to Business guide highlights the best products for you to boost your productivity at home, on the road, at the office, or in the classroom.

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 News Articles

Resources

PCW Evaluation Team

Azadeh Williams

HP OfficeJet Pro 8730

A smarter way to print for busy small business owners, combining speedy printing with scanning and copying, making it easier to produce high quality documents and images at a touch of a button.

Andrew Grant

HP OfficeJet Pro 8730

I've had a multifunction printer in the office going on 10 years now. It was a neat bit of kit back in the day -- print, copy, scan, fax -- when printing over WiFi felt a bit like magic. It’s seen better days though and an upgrade’s well overdue. This HP OfficeJet Pro 8730 looks like it ticks all the same boxes: print, copy, scan, and fax. (Really? Does anyone fax anything any more? I guess it's good to know the facility’s there, just in case.) Printing over WiFi is more-or- less standard these days.

Ed Dawson

HP OfficeJet Pro 8730

As a freelance writer who is always on the go, I like my technology to be both efficient and effective so I can do my job well. The HP OfficeJet Pro 8730 Inkjet Printer ticks all the boxes in terms of form factor, performance and user interface.

Michael Hargreaves

Windows 10 for Business / Dell XPS 13

I’d happily recommend this touchscreen laptop and Windows 10 as a great way to get serious work done at a desk or on the road.

Aysha Strobbe

Windows 10 / HP Spectre x360

Ultimately, I think the Windows 10 environment is excellent for me as it caters for so many different uses. The inclusion of the Xbox app is also great for when you need some downtime too!

Mark Escubio

Windows 10 / Lenovo Yoga 910

For me, the Xbox Play Anywhere is a great new feature as it allows you to play your current Xbox games with higher resolutions and better graphics without forking out extra cash for another copy. Although available titles are still scarce, but I’m sure it will grow in time.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?