Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

readdir()

Example

List all entries in the images directory, and then close it.

<?php
$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 . “<br>”;
    }
    closedir($dh);
  }
}
?>

Result:

filename: cat.gif
filename: dog.gif
filename: horse.gif

Definition and Usage

The readdir() function returns the name of the next entry in a directory.

Syntax

readdir(dir)

Parameter Values

Parameter

Description

dir

Optional. Specifies the directory handle resource previously opened with opendir(). If not provided, the function assumes the last directory handle opened by opendir().

Technical Details

Return Value:

Returns the entry name (filename) on success, or FALSE on failure.

PHP Version:

4.0+