Recipe 24.7. Processing All Files in a Directory

24.7.1. Problem

You want to iterate over all files in a directory. For example, you want to create a <select/> box in a form that lists all the files in a directory.

24.7.2. Solution

Use a DirectoryIterator to get each file in the directory, as in Example 24-20.

Processing all files in a directory

<?php echo "<select name='file'>\n"; foreach (new DirectoryIterator('/usr/local/images') as $file) {     echo '<option>' . htmlentities($file) . "</option>\n"; } echo '</select>'; ?>

24.7.3. Discussion

The DirectoryIterator yields one value for each element in the directory. That value is an object with some handy characteristics. The object's string representation is the filename (with no leading path) of the directory element. For example, if /usr/local/images contains the files cucumber.gif and eggplant.png, Example 24-20 prints:

<select name='file'> <option>.</option> <option>..</option> <option>cucumber.gif</option> <option>eggplant.png</option> </select>

A DirectoryIterator yields an object for all directory elements, including . (current directory) and .. (parent directory). Fortunately, that object has some methods that help us identify what it is. The isDot( ) method returns true if it's either . or ... Example 24-21 uses isDot( ) to prevent those two entries from showing up in the output.

Removing . and .. from output

<?php echo "<select name='file'>\n"; foreach (new DirectoryIterator('/usr/local/images') as $file) {     if (! $file->isDot()) {         echo '<option>' . htmlentities($file) . "</option>\n";     } } echo '</select>'; ?>

Table 24-4 lists the other methods available on the objects that a DirectoryIterator yields.

Table DirectoryIterator object information methods

Method Name

Return value


isDir( )

Is the element a directory?


isDot( )

Is the element either . or ..?


isFile( )

Is the element a regular file?


isLink( )

Is the element a link?


isReadable( )

Is the element readable?


isWritable( )

Is the element writable?


isExecutable( )

Is the element executable?


getATime( )

The last access time of the element.


getCTime( )

The creation time of the element.


getMTime( )

The last modification time of the element.


getFilename( )

The filename (without leading path) of the element.


getPathname( )

The full pathname of the element.


getPath( )

The leading path of the element.


getGroup( )

The group ID of the element.


getOwner( )

The owner ID of the element.


getPerms( )

The permissions of the element, as an octal value.


getSize( )

The size of the element.


getType( )

The type of the element (dir, file, link, etc.).


getInode( )

The inode number of the element.


The data that the functions in Table 24-4 report come from the same underlying system calls as the data that the functions in Table 24-1 report, so the same cautions on differences between Unix and Windows apply.

24.7.4. See Also

Documentation on DirectoryIterator at

PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: