English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

PHP dir() function usage and example

PHP Directory Reference Manual

The dir() function returns an instance of the Directory class

Syntax

dirhandle bool dir ( string $directory )

Definition and usage

The dir() function opens a directory handle and returns an object. This object contains three methods, namely read(), rewind(), and close().

Parameter

NumberParameters and descriptions
1

directory(必需)

The directory to be opened.

Return value

 If successful, returns an instance of the Directory class, returns NULL if the parameter is incorrect, and returns FALSE for other error conditions.

Online example

The following is the usage of the dir() function:

<?php
   $d = dir("/var/www/");
   echo "Handle: " . $d->handle . "\n";
   echo "Path: " . $d->path . "\n";
   
   while (false !== ($entry = $d->read())) {
      echo $entry."\n";
   }
   $d->close();
?>

Output result:

Handle: Resource id #5 Path: /var/www/ .. cgi-bin html .