PHP closedir() function with example

PHP closedir() function with example

PHP closedir() function: Here, we will find out about the PHP closedir() function with its syntax, parameters, returns type, and example.

PHP closedir() function

The full type of closedir is “Close Directory“, the function closedir() is utilized to close an opened index.

Syntax:

closedir(dir_handle); 

Parameter(s):

dir_handle – it is a discretionary parameter which is utilized to determine the index handle asset in the event that we don’t indicate the catalogue handle asset – it expects the last connection opened with opendir() function.

Return value:

  • It returns nothing.

Example: PHP code to open a registry, read its documents, and close it

<?php

$path = "/home";

//checking whether $path is a directory or not
//then, opening the directory and reading its files
if (is_dir($path)){
  if ($dh = closedir($path)){
    while (($file = readdir($dh)) !== false){
      echo "File:" . $file . "<br/>";
    }
    //closing the directory
    closedir($dh);
  }
}
?>

Output

File:..
File:main.php
File:.

Reference: PHP closedir() function

Recent Posts

error: Alert: Content is protected!!