English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP closedir() Function Usage and Example

PHP Directory Reference Manual

The closedir() function is used to close directory handles

Syntax

void closedir ( resource $dir_handle );

Definition and Usage

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

Parameter

NumberParameters and Description
1

dir_handle (required)

The resource handle of the directory, previously opened by opendir(). If the directory handle is not specified, it is assumed to be the last handle opened by opendir().

Return value

Returns TRUE on success, FALSE on failure.

Online Example

The following is the usage of this function: open a directory, read its contents, and then close it:

<?php
   $dir = opendir("/var/www/images");
   while (($file = readdir($dir)) !== false) {
      echo "filename: " . $file . "<br />";
   }
   
   closedir($dir);
?>

Output result:

filename: .
filename: ..
filename: logo.gif
filename: mohd.gif