+

PHP closedir () function

Syntax:
closedir ($dir_handle) 
Parameters used:PHP`s closedir() function accepts only one parameter as described below.
  • $dir_handle : This is an optional parameter that specifies a directory handle resource previously opened with opendir(). If this parameter is not specified, the last link opened by opendir() is considered closed by closedir().
Return value:does not return any value. Errors and Exceptions :
  • The directory descriptor sent as a parameter to the closedir() function must have been previously opened by the opendir() function.
  • If the dir_handle parameter is not specified, the last link opened with opendir() is considered closed with the closedir() function.
  • The following programs illustrate the closedir() function: Program 1:  
    // Open the directory $dir_handle = opendir ( "/ user / gfg / doc s / " ); if ( is_resource ( $dir_handle )) { echo ( "Directory Opened Successfully." ); // close the directory closedir ( $dir_handle ); }
    else
    { echo ( "Directory Cannot Be Opened." ); }  
    ?>
    Output:
    Directory Opened Successfully. 
    Program 2 :  
    // open the directory and read 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 > " ; } // close the directory closedir ( $dir_handle ); }
    else
    { echo ( "Directory Cannot Be Opened." ); }  
    ?>
    Output:
    File Name: sample.docx 
    Link: http: //php.net/manual/en/function.closedir.php
    Get Solution for free from DataCamp guru