PHP Function Reference

PHP closedir() Function



The PHP closedir() function closes the directory stream indicated by dir_handle. The stream must have previously been opened by opendir() function.

Syntax

closedir(dir_handle)

Parameters

dir_handle Optional. Specify the directory handle resource previously opened with opendir(). If this parameter is not specified, the last link opened by opendir() is assumed.

Return Value

No value is returned.

Example: closedir() example

The example below shows the usage of closedir() function.

<?php
$dir = "/temp/images";

//opens the directory, and read its contents
if (is_dir($dir)){
  if ($dh = opendir($dir)){
    
    //reading all entry from directory handle
    while (($file = readdir($dh)) !== false){
      echo "File Name: ".$file."\n";
    }
    
    //closing the directory handle
    closedir($dh);
  }
}
?>

The output of the above code will be:

File Name: fig1.png
File Name: fig2.png
File Name: Photo.jpg
File Name: .
File Name: ..
File Name: error.jpeg

❮ PHP Directory Reference