What is PHP closedir() Function?
closedir() function can close a directory handle. To perform file and folder related different tasks, we first open a directory where the files and other folders exists. When we open a directory with opendir() function, it provides us a directory handle. Using it, we can perform other related operations. At the end, we need to close the directory handle resource and closedir() function does this.
“closedir” is short for “Close Directory”.
Syntax:
closedir(directory_handle)
Parameters:
The Function has 1 parameter which is optional-
dir_handle (Optional): It is a directory handle that was opened previously by the opendir() function. Check example 1. If you don’t mention the parameter, the closedir() function uses the last opened directory handle by opendir() function. Check example 2.
Return Values:
The function returns no value.
Examples:
Example 1: Closing a directory handle with closedir() function-
<?php
$dir_handle = opendir("./file");
closedir($dir_handle);
?>
Explanation:
The opendir() function opens the directory handle $dir_handle and the closedir() function closes the directory stream.
Example 2: Closing a directory handle without mentioning the directory handle-
<?php
$dir_handle = opendir("./file");
closedir();
?>
Explanation:
Though we don’t mention the directory handle explicitly in the closedir() function, the function doesn’t throw any error. In fact, it closes a directory handle. And, the handle is the one that is opened by the opendir() function in the previous line.
Example 3: Confirming before closing a directory handle-
<?php
if(is_resource($dir_handle)){
echo "Do some directory related operations like read all the files, folders in it.";
closedir($dir_handle);
if(is_resource($dir_handle)) echo "Still open"; else echo "Closed";
}else{
echo("Error! It is not a directory handle.");
}
?>
Output:
Error! It is not a directory handle.
Explanation:
$dir_handle is not a directory handle. No opendir() function created this directory handle. is_resource() function detects this and return FALSE. So you see the above output.
Example 4: Closing a directory handle multiple times-
<?php
$dir_handle = opendir("./file");
closedir($dir_handle);
/*
Do some operations
*/
closedir($dir_handle);
?>
Output:
Fatal error: Uncaught TypeError: closedir(): supplied resource is not a valid Directory resource in D:\xampp\htdocs\closedir.php:29 Stack trace: #0 D:\xampp\htdocs\closedir.php(7): closedir(Resource id #6) #1 {main} thrown in D:\xampp\htdocs\closedir.php on line 7
Explanation:
As the directory handle $dir_handle is closed on line 3, there is no resource with that name in the memory. So, next time on line 7, when we try to close the same directory handle, PHP returns error.
Best Practices on closedir() Function:
- When you open a directory, it provides you a directory handle with which you can do different operations. This resource remains open until you close this explicitly. So, closedir() function helps to optimize resources by closing the directory handle. Note, directory stream though automatically closed at the end of the script, but we follow the best practice.
- Before closing a file handle, check whether it is a resource or not with is_resource() function. It saves you from displaying error. Check example 3.
Caution:
- If you try to close a directory handle that is already closed, you’ll get an Uncaught TypeError. Check example 4. For this reason, you need to check whether the provided directory handle is a resource before closing it. Check example 3.
PHP Version Support:
PHP 4, PHP 5, PHP 7, PHP 8
Summary: PHP closedir() Function
When you open a directory handle, you need to close it after using it at last. closedir() function helps you to accomplish it which is a built-in directory function.