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

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP opendir() Function Usage and Example

PHP Directory Reference Manual

The opendir() function opens a directory handle

Syntax

resource opendir(string $path[, resource $context]);

Definition and Usage

 Open a directory handle that can be used in subsequent closedir(), readdir(), and rewinddir() calls.

Parameter

Serial NumberParameters and Description
1

path (required)

The directory path to be opened

2

context (optional)

Specify the environment of the directory handle. context is a set of options that can modify the behavior of the directory stream.

Return value

If successful, it returns a resource handle of the directory, and if it fails, it returns FALSE.
If path is not a valid directory or cannot be opened due to permission restrictions or file system errors, opendir() returns FALSE and generates a PHP E_WARNING level error message. You can add the '@' symbol before opendir() to suppress the output of the error message.

Online Example

The following is the usage of this function: open a directory, read its contents, and then close it:

<?php
   $dir = opendir("/var/www/images");
   while (($file = readdir($dir)) !== false) {
      echo "filename: " . $file . "<br" />";
   }
   closedir($dir);
?>

Output result:

filename: .
filename: ..
filename: logo.gif
filename: mohd.gif