• PHP Video Tutorials

PHP - Function opendir()



Syntax

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

Definition and Usage

It opens up a directory handle to be used in subsequent closedir(), readdir(), and rewinddir() calls.

Parameters

Sr.No Parameter & Description
1

path(Required)

The directory path that is to be opened

2

context(Optional)

Specifies the context of the directory handle. Context is a set of options that can modify the behavior of a stream.

Return Value

It returns a directory handle resource on success, or FALSE on failure.

Example

Following is the usage of this function −

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

This will produce the following result −

filename: .
filename: ..
filename: logo.gif
filename: mohd.gif
php_function_reference.htm
Advertisements