Приглашаем посетить
Почтовые индексы (post.niv.ru)

Browsing the File System

Previous
Table of Contents
Next

Browsing the File System

$d = dir('.');
while (false !== ($file = $d->read()))


Since PHP 3, PHP comes with a built-in class. This sounds strange because OOP (object-oriented programming) did not become "mainstream" in PHP before version 5; however, this special class offers several methods to access the file system. It is possible to get a list of all entries in a directory.

Reading a Directory's Contents with PHP's dir Class (dir.php)
<?php
  $d = dir('.');
  while (false !== ($file = $d->read())) {
    echo htmlspecialchars($file) . '<br />';
  }
  $d->close();
?>

You instantiate the class without the keyword new, just by providing the desired path as a parameter. Then the method read() iterates through the list of entries. The preceding code does exactly this and prints out the contents of the current directory.

Note that the two directory entries . (current directory) and .. (parent directory, if you are not in the root directory) are printed out.


Previous
Table of Contents
Next