PHP closedir() Function

Example

Open a directory, read its contents, 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 closedir() function closes a directory handle.

Syntax

closedir(dir_handle);
Parameter Description
dir_handle

Optional. Specify the directory handle resource opened by opendir() previously.

If this parameter is not specified, the last link opened by opendir() is used.

Technical Details

Return Value: -
PHP Version: 4.0+