PHP fclose() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, fclose() closes a file that was opened with fopen(). It tells PHP you are finished with the file so it can free the resource and finish writing any saved data.

PHP closes open files automatically when the script ends, but closing them yourself is a good habit. It frees memory sooner and releases file locks, which matters in long scripts that open many files.

Syntax

Call fclose() with the values you need. Start with this line.

PHP
fclose($handle);
CallWhat it does
fclose($handle)Closes the file handle and returns true or false.
fclose(fopen(__DIR__ . "/demo.txt", "w"))Creates an empty file by opening and closing it right away.
if (is_resource($handle)) { fclose($handle); }Closes the handle only if it is still open.

fclose() works on any stream opened by fopen(), fsockopen(), or popen(). Since PHP 8, using a handle after fclose() throws a TypeError instead of only giving a warning.

Library declarations
PHP
fclose(resource $stream): bool

Parameters

ParameterDescription
streamRequired. The file pointer returned by fopen() or a similar function.

Return Value

Return ValueDescription
booltrue on success, false on failure.

PHP Examples

Example 1 — Write to a File and Close It

Open a file for writing, add some text, then close it with fclose().

PHP
<?php
$file = __DIR__ . "/demo.txt";

$handle = fopen($file, "w");
fwrite($handle, "Hello CodeToFun" . PHP_EOL);
fclose($handle);

echo file_get_contents($file);

unlink($file);

How It Works

  1. fopen() opens the file in write mode and returns a handle.
  2. fwrite() writes one line of text through the handle.
  3. fclose() finishes the write and releases the file.
  4. file_get_contents() reads the file back so we can see the text.

Example 2 — Using a Handle After fclose()

Once a handle is closed it is not valid any more. In PHP 8 an error is thrown if you try to use it.

PHP
<?php
$file = __DIR__ . "/demo.txt";
$handle = fopen($file, "w");

var_dump(is_resource($handle));

fclose($handle);

try {
    fwrite($handle, "more text");
} catch (TypeError $e) {
    echo "Error: ", $e->getMessage(), PHP_EOL;
}

unlink($file);

How It Works

  1. The handle is a valid resource right after fopen().
  2. fclose() closes it, so it can not be written to again.
  3. fwrite() on the closed handle throws a TypeError in PHP 8.
  4. The catch block prints the error message instead of stopping the script.

Example 3 — Read a File Line by Line

Open a file, read each line with fgets(), and close the handle when the loop is done.

PHP
<?php
$file = __DIR__ . "/names.txt";
file_put_contents($file, "Alice" . PHP_EOL . "Bob" . PHP_EOL . "Carol" . PHP_EOL);

$handle = fopen($file, "r");

while (($line = fgets($handle)) !== false) {
    echo trim($line), PHP_EOL;
}

fclose($handle);
unlink($file);

How It Works

  1. We create a small text file with three names.
  2. fopen() opens the file in read mode.
  3. fgets() reads one line each time, and trim() removes the line break.
  4. After the loop ends, fclose() closes the handle and unlink() removes the file.

Frequently Asked Questions

It closes a file handle opened with fopen(). It finishes any pending writes and frees the resource.
PHP closes open files when the script ends, but you should still call fclose() yourself. It frees resources sooner and releases file locks.
In PHP 8 and newer a TypeError is thrown. In older versions you only get a warning and the call fails.
No. The second call fails because the handle is already closed. Use is_resource() to check before closing if you are not sure.
fclose() closes an open file handle but keeps the file on the disk. unlink() deletes the file from the disk.

Next: fdatasync()

fdatasync() forces the data you wrote to an open file to be saved on the physical disk. It.

fdatasync() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful