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

Example

isDir( )

Is the element a directory?

false

isDot( )

Is the element either . or ..?

false

isFile( )

Is the element a regular file?

true

isLink( )

Is the element a link?

false

isReadable( )

Is the element readable?

TRue

isWritable( )

Is the element writable?

TRue

isExecutable( )

Is the element executable?

false

getATime( )

The last access time of the element.

1144509622

getCTime( )

The creation time of the element.

1144509600

getMTime( )

The last modification time of the element.

1144509620

getFilename( )

The filename (without leading path) of the element.

eggplant.png

getPathname( )

The full pathname of the element.

/usr/local/images/eggplant.php

getPath( )

The leading path of the element.

/usr/local/images

getGroup( )

The group ID of the element.

500

getOwner( )

The owner ID of the element.

1000

getPerms( )

The permissions of the element, as an octal value.

16895

getSize( )

The size of the element.

328742

getType( )

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

file

getInode( )

The inode number of the element.

28720


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 http://www.php.net/~helly/php/ext/spl/classDirectoryIterator.html.




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

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net