dirDescriptiondir() is a pseudo-object-oriented wrapper for all the directory functions that work with directory streams. It is certainly one of PHP's oddball functions - an early experiment in implementing object-oriented features for PHP. (See the notes for an anecdote from Jim Winstead, the author of the function.) When the function is called, it opens a directory stream to directory dir and returns an object. When the directory stream is opened, a resource pointer for the stream is created. This pointer is stored as a class property called handle and the pathname of the directory is stored as a class property called path . In addition to these properties, the object contains three methods called read() , rewind() , and close() . These methods are wrappers for the readdir() , rewinddir() , and closedir() functions, and behave in a similar fashion. The methods require no arguments and are called using the following syntax: $object_name->method_name (); The function returns FALSE if the open_basedir configuration directive is set and directory dir is not below the directory specified in open_basedir. The function also returns FALSE if a directory stream for directory dir could not be opened. NoteThe following quote from Jim Winstead, author of the dir function, discusses how the function came to be: “"The object-oriented interface was just an experiment to see if it could be done. At the time, the object-oriented features of PHP were new, and there was nothing else that implemented an object-oriented PHP interface at the C level. Apparently I intended to write a object-oriented interface to gd at one point. (See http://marc.theaimsgroup.com/?l=php-dev&m=90279104404070&w=2.) I'm not sure why that never happened."” NoteIf dir() fails because a directory stream could not be opened to the directory, a warning is generated. To suppress the warning, place a single @ before the function call: $dir_object = @ dir ('directory_path'); ExampleExample 244. List all directories in the current working directory // For PHP3, use: $directory = '.'; $directory = getcwd (); $dir_object = @ dir ($directory) or die ("Could not open a directory stream for <i>$directory</i>"); // Display information about the directory stream // print_r() was introduced in PHP 4 print_r ($dir_object); while ($entry = $dir_object->read ()) { print "<br />$entry"; } $dir_object->close ();
PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing
(Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch).
This material may be distributed only subject to the terms and conditions set forth
in the Open Publication License, v1.0 or later (the latest version is presently available at
http://www.opencontent.org/openpub/).
The authors of this book have elected not to choose any options under the OPL. This online book was obtained
from http://www.fooassociates.com/phpfer/
and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4
for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All
persons and entities shall have neither liability nor responsibility to any person or entity with respect to
any loss or damage arising from the information contained in this book.
|