PHP | closedir( ) Function
The closedir() function in PHP is an inbuilt function which is used to close a directory handle. The directory handle to be closed is sent as a parameter to the closedir() function and the closedir() closes the directory handle. The directory handle must be previously opened by the opendir() function.
Syntax:
closedir($dir_handle)
Parameters Used: The closedir() function in PHP accepts only one parameter as described below.
- $dir_handle: It is an optional parameter which specifies the directory handle resource previously opened with opendir().If this parameter is not specified, the last link opened by opendir() is assumed and closed by closedir().
Return Value: It does not return any value.
Errors And Exceptions:
- The directory handle sent as a parameter to the closedir() function must be previously opened by the opendir() function.
- If the dir_handle parameter is not specified, the last link opened by opendir() is assumed and closed by closedir() function.
Below programs illustrate the closedir() function:
Program 1:
<?php // Opening a directory $dir_handle = opendir("/user/gfg/docs/"); if(is_resource($dir_handle)) { echo("Directory Opened Successfully."); // closing the directory closedir($dir_handle); } else{ echo("Directory Cannot Be Opened."); } ?> |
Output:
Directory Opened Successfully.
Program 2:
<?php // opening a directory and reading its contents $dir_handle = opendir("user/gfg/sample.docx"); if(is_resource($dir_handle)) { while(($file_name = readdir($dir_handle)) == true) { echo("File Name: " . $file_Name); echo "<br>" ; } // closing the directory closedir($dir_handle); } else{ echo("Directory Cannot Be Opened."); } ?> |
Output:
File Name: sample.docx
Reference: http://php.net/manual/en/function.closedir.php
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to get the function name from within that function using JavaScript ?
- How to get the function name inside a function in PHP ?
- D3.js | d3.mean() function
- PHP | next() Function
- p5.js | nfp() Function
- CSS | url() Function
- PHP | end() Function
- p5.js | nfs() Function
- p5.js | arc() Function
- D3.js | d3.sum() function
- PHP | each() Function
- PHP | abs() Function
- p5.js | hue() function
- D3.js | d3.map.set() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



