Tutorial for Closing File in PHP

Closing file in php is all related to fclose( ) Function. With the help of this code we can run closing function in php.

What is fclose( ) Function in php?

In PHP, the fclose() method is used to close a file referenced by an open file pointer. If the method fclose() is successful, it returns true; otherwise, it returns false. It accepts the file that needs to be closed as an input and closes it.

Syntax:

bool fclose( $file )

Parameters:

The $file argument is the only one accepted by PHP’s fclose() function. The file that has to be closed is specified by this argument.

Return Value:

If it succeeds, it returns true; if it fails, it returns false.

Errors And Exception:

  • If you want to read the contents of a file that was written using the fwrite() function, you must first shut it with the fclose() function.
  • For remote files, PHP’s fclose() method does not work. It only works with files that the server’s filesystem can access.

Let’s take examples to explain this function:

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

Example 1:-

Output:

Example 2:- The file test.txt in the software below includes just one line: “This file contains only one line.”

Output:

Tagged : / / / / / / / / / / / /