Latest web development tutorials

PHP dir () function

PHP Directory PHP Directory Reference

Examples

Use dir () function:

<?php
$d = dir(getcwd());

echo "Handle: " . $d->handle . "<br>";
echo "Path: " . $d->path . "<br>";

while (($file = $d->read()) !== false){
echo "filename: " . $file . "<br>";
}
$d->close();
?>

result:

Handle: Resource id #2
Path: /etc/php
filename: .
filename: ..
filename: ajax.gif
filename: books.xml
filename: cdcatalog.xml
filename: cd_catalog.xml
filename: default.html
filename: demo_array.html
filename: demo_array.htm
...
...
...


Definition and Usage

dir () function returns an instance of the Directory class. This function is used to read a directory that contains the following:

  • To open a given directory
  • dir () and the path of the handle two properties are available
  • handle and path attribute has three methods: read (), rewind () and close ()

grammar

dir( directory,context );

参数 描述
directory 必需。规定要打开的目录。
context 可选。

technical details

return value: Return an instance of the Directory class. It failed to return FALSE.
PHP version: 4.0+


PHP Directory PHP Directory Reference