In this article, you will learn how to close an opened directory in PHP. The closedir() function in PHP closes a directory handle.
what is the syntax of the CLOSEDIR() function in php?
closedir(dir)
Parameter | Description |
---|---|
dir | Optional. Specifies the directory handle resource previously opened with opendir(). If this parameter is not specified, the last link opened by opendir() is assumed |
examples of the CLOSEDIR() function
Example 1. In this example, we open a directory, read its contents, then close.
<?php
$dir = "/images/";
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
echo "filename:" . $file . "<br>";
}
closedir($dh);
}
}
?>