PHP opendir() Function

Definition

The opendir() function opens a directory handle.

Syntax

PHP opendir() Function has the following syntax.

opendir(path,context);

Parameter

ParameterIs RequiredDescription
pathRequired.Directory path to be opened
contextOptional.Context of the directory handle.

Return

PHP opendir() Function returns the directory handle resource on success. FALSE on failure.

Example

Open a directory, read its contents, then close:


<?php//  w  w  w. j  a  v  a  2s  .c o  m
$dir = "/images/";

// Open a directory, and read its contents
if (is_dir($dir)){
  if ($dh = opendir($dir)){
    while (($file = readdir($dh)) !== false){
      echo "filename:" . $file . "\n";
    }
    closedir($dh);
  }
}
?>




















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions