PHP Function Reference

PHP fclose() Function



The PHP fclose() function closes an open file.

Syntax

fclose(stream)

Parameters

stream Required. Specify the file pointer to close. It must be valid, and must point to a file successfully opened by fopen() or fsockopen().

Return Value

Returns true on success or false on failure.

Example:

Lets assume that we have a file called test.txt. This file contains following content:

This is a test file.
It contains dummy content.

In the example below, the file is opened using fopen() function. After performing the reading operation, it is closed using fclose() function.

<?php
//open the file in read mode
$fp = fopen("test.txt", "r") or 
         die("Unable to open file!");

//reading the file line by line
//and displaying the read content
while(!feof($fp)) {
  echo fgets($fp);
}

//close the file
fclose($fp);
?>

The output of the above code will be:

This is a test file.
It contains dummy content.

❮ PHP Filesystem Reference