Directory Functions
The following functions are used to manipulate directories. For example, to open the current directory and read in all the entries, you can do something like this:
$handle = opendir('.');
while($entry = readdir($handle)) {
echo "$entry<br>\n";
}
closedir($handle);PHP supports a dir class that represents a
directory. Here’s an example of using this object-oriented
approach to reading a directory:
$d = dir("/etc");
echo "Handle: ".$d->handle."<br>\n";
echo "Path: ".$d->path."<br>\n";
while($entry=$d->read( )) {
echo $entry."<br>\n";
}
$d->close( );In addition to the read( ) and close( ) methods, the dir class supports a
rewind( ) method.
Here are the directory functions supported by PHP:
int chdir(string directory)Change the current directory
void closedir([int dir_handle])Close the directory connection identified by the
dir_handle, or a previously opened directory if not specifiedclass dir(string directory)Return a class with handle and path properties as well as read, rewind, and close methods
int opendir(string directory)Open a directory and return a
dir_handlestring readdir(int dir_handle)Read a directory entry from
dir_handlevoid rewinddir(int dir_handle)Rewind
dir_handleback to the start
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access