PHP | fclose( ) Function

The fclose() function in PHP is an inbuilt function which is used to close a file which is pointed by an open file pointer. The fclose() function returns true on success and false on failure. It takes the file as an argument which has to be closed and closes that file.

Syntax:

bool fclose( $file )

Parameters: The fclose() function in PHP accepts only one parameter which is $file. This parameter specifies the file which has to be closed.

Return Value: It returns true on success and false on failure.

Errors And Exception:

  1. A file has to be closed first using the fclose() function if it has been written via fwrite() function and you have to read the contents of the file.
  2. The fclose() function in PHP doesn’t works for remote files.It only works on files which are accessible by the server’s filesystem.
  3. Examples:

    Input : $check = fopen("gfg.txt", "r");
            fclose($check);
    Output : true
    
    Input: $check = fopen("singleline.txt", "r");
           $seq = fgets($check);
           while(! feof($check))
           {
             echo $seq ;
             $seq = fgets($check);
           }
           fclose($check);
    Output:true
    

    Below programs illustrate the fclose() function:

    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    // opening a file using fopen() function
    $check = fopen("gfg.txt", "r");
      
    // closing a file using fclose() function
    fclose($check);
      
    ?>

    chevron_right

    
    

    Output:

    true

    Program 2: In the below program the file named singleline.txt contains only a single line “This file consists of only a single line”.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    // a file is opened using fopen() function
    $check = fopen("singleline.txt", "r");
    $seq = fgets($check);
      
    // Outputs a line of the file until
    // the end-of-file is reached
    while(! feof($check))
    {
      echo $seq ;
      $seq = fgets($check);
    }
      
    // the file is closed using fclose() function
    fclose($check);
      
    ?>

    chevron_right

    
    

    Output:

    This file consists of only a single line.
    

    Reference:
    http://php.net/manual/en/function.fclose.php



    My Personal Notes arrow_drop_up

    I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

    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.




    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.