PHP Tutorial - PHP closedir() Function






Definition

The closedir() function closes a directory handle.

Syntax

PHP closedir() Function has the following syntax.

closedir(dir_handle);

Parameter

ParameterIs RequiredDescription
dir_handleOptional.Directory handle resource previously opened with opendir(). If not specified, the last link opened by opendir() is used

Example

Open a directory, read its contents, then close:


<?php//from   w w w  . j  a v a 2s  .  co m
$dir = "/data/";

if (is_dir($dir)){
  if ($dh = opendir($dir)){
    while (($file = readdir($dh)) !== false){
      echo "filename:" . $file . "\n";
    }
    closedir($dh);
  }
}
?>