Directory routines demo
- Functions demonstrated
- <?opendir("/path/dir")>
<?closedir()>
<?readdirdir()>
<?rewinddir()>
It is often important to be able to "walk" through
a directory and do something based on the files in the
directory. PHP includes functions that will let you do just
that. The first thing to do is to open the directory. The
syntax for doing is:
<?opendir("/path/dir")>
Once opened, the readdir() function may be used to
read entries in the directory sequentially. The first call
to readdir() will return the first entry and the second
call the second entry, etc.
The rewinddir() function can be used to move the
directory pointer back to the beginning resulting in the
next call to readdir() returning the first entry in the
directory.
And, at the end, the closedir() function should be
called.
Below is a simple example which reads the contents of the
directory in which the PHP program resides.
$dir = readdir()>
$dir = readdir()>
closedir();