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.


.
..
mlog_today.inc
log.readme
demo_dns.html
mlog_yesterday.inc
demo_rand.phtml
demo_last.phtml
demo_date.phtml
url_main.phtml
pgurl_main.phtml
README
mlog_total.inc
log.phtml
clock.phtml
mylog_yesterday.inc
rwho.phtml
survey.phtml
setsince.phtml
mlog_ref.inc
mylog_ref.inc
sybase
url.readme
pgurl.readme
mylog_today.inc
demo_echo.phtml
demo_main.phtml
demo_misc.phtml
guestbook.phtml
demo_dbm.phtml
demo_dir.phtml
demo_msql.phtml
pgurl_cat.phtml
pgurl_add.phtml
demo_stat.phtml
demo_str.phtml
demo_reg.phtml
demo_synt.phtml
mlog.phtml
mylog.phtml
mlog_top.inc
mylog_top.inc
url_cat.phtml
url_add.phtml
mail.phtml
mylog_total.inc
closedir();