Home »
PHP
PHP closedir() function with example
PHP closedir() function: Here, we are going to learn about the PHP closedir() function with its syntax, parameters, returns type, and example.
Submitted by IncludeHelp, on August 13, 2019
PHP closedir() function
The full form of closedir is "Close Directory", the function closedir() is used to close an opened directory.
Syntax:
closedir(dir_handle);
Parameter(s):
- dir_handle – it is an optional parameter which is used to specify the directory handle resource if we do not specify the directory handle resource – it assumes the last link opened with opendir() function.
Return value:
It returns nothing.
Example: PHP code to open a directory, read its files, 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
TOP Interview Coding Problems/Challenges