Directory functionsDirectories
For related functions such as dirname,
is_dir, mkdir, and
rmdir, see the
Filesystem section.
chrootchange the root directoryDescriptionboolchrootstringdirectory
Changes the root directory of the current process to
directory. Returns &false; if unable to
change the root directory, &true; otherwise.
It's not wise to use this function when running in a webserver
environment, because it's not possible to reset the root
directory to / again at the end of the request. This function
will only function correct when running as CGI this way.
chdirchange directoryDescriptionboolchdirstringdirectory
Changes PHP's current directory to
directory. Returns &false; if unable to
change directory, &true; otherwise.
dirdirectory classDescriptiondirdirstringdirectorystringpathstringreadvoidrewindvoidclose
A pseudo-object oriented mechanism for reading a directory. The
given directory is opened. Two properties
are available once the directory has been opened. The handle
property can be used with other directory functions such as
readdir, rewinddir and
closedir. The path property is set to path
the directory that was opened. Three methods are available:
read, rewind and close.
Please note the fashion in which dir's
return value is checked in the example below. We are explicitly
testing whether the return value is identical to (equal to and of
the same type as--see
Comparison Operators for more information) &false; since
otherwise, any directory entry whose name evaluates to &false; will
stop the loop.
dir example
handle." \n";
echo "Path: ".$d->path." \n";
while (false !== ($entry = $d->read())) {
echo $entry." \n";
}
$d->close();
]]>
The order in which directory entries are returned by the read method is
system-dependent.
This defines the internal class
Directory, meaning that you will not be
able to define your own classes with that name. For a full list
of predefined classes in PHP, please see Predefined Classes.
closedirclose directory handleDescriptionvoidclosedirresourcedir_handle
Closes the directory stream indicated by
dir_handle. The stream must have previously
been opened by opendir.
getcwdgets the current working directoryDescriptionstringgetcwd
Returns the current working directory.
opendiropen directory handleDescriptionresourceopendirstringpath
Returns a directory handle to be used in subsequent
closedir, readdir, and
rewinddir calls.
If path is not a valid directory or the
directory can not be opened due to permission restrictions or
filesystem errors, opendir returns &false; and
generates a PHP error. You can suppress the error output of
opendir by prepending `@' to the front of
the function name.
opendir example
]]>
See also is_dir.readdirread entry from directory handleDescriptionstringreaddirresourcedir_handle
Returns the filename of the next file from the directory. The
filenames are returned in the order in which they are stored by
the filesystem.
Please note the fashion in which readdir's
return value is checked in the examples below. We are explicitly
testing whether the return value is identical to (equal to and of
the same type as--see Comparison
Operators for more information) &false; since otherwise,
any directory entry whose name evaluates to &false; will stop the
loop.
List all files in a directory
]]>
Note that readdir will return the .
and
.. entries. If you don't want these, simply strip
them out:
List all files in the current directory and strip out .
and ..
]]>
See also is_dir.rewinddirrewind directory handleDescriptionvoidrewinddirresourcedir_handle
Resets the directory stream indicated by
dir_handle to the beginning of the
directory.