W3docs

fclose()

The fclose() function in PHP is used to close an open file pointer. It's a crucial function for server administrators and web developers who want to manage

Introduction to PHP fclose() Function

The fclose() function closes a file pointer that was previously opened with fopen(). A file pointer (also called a handle or stream resource) is the value PHP gives you when it opens a file; every read and write operation uses it to track your current position in the file.

Closing a handle when you are done matters for two reasons:

  • It flushes the write buffer. PHP and the operating system don't necessarily write every byte to disk immediately — they hold data in a buffer for performance. fclose() forces any buffered output to be written, so a file you forgot to close may end up missing its last bytes.
  • It frees system resources. Each open file consumes an operating-system file descriptor. A long-running script (or a loop that opens files without closing them) can exhaust the descriptor limit and start failing with "Too many open files".

PHP closes every still-open handle automatically when the script ends, so you rarely lose data in a short script. But you should still close handles explicitly — especially in loops and long-running processes — so resources are released as early as possible.

Syntax

fclose(resource $stream): bool
  • $stream — the open file pointer you want to close.

Parameters

The fclose() function takes one required parameter:

  1. $stream: The file pointer you want to close. It must be a valid, open resource returned by fopen(), fsockopen(), popen(), or a similar function. Passing an invalid or already-closed resource triggers a TypeError (PHP 8+) or a warning (PHP 7), and the function returns false.

Return Values

Returns true on success or false on failure.

Examples

Example 1: Open, write, and close a file

The most common pattern is to open a file, work with it, then close it. Closing flushes the buffered text to disk:

Write to a file and close the handle

<?php

$handle = fopen("notes.txt", "w");   // open for writing (creates/empties the file)
fwrite($handle, "Hello, fclose!\n");
fclose($handle);                      // flush + release the handle

echo "File written and closed.";

Output:

File written and closed.

Example 2: Always check that the file opened

fopen() returns false if the file can't be opened (missing file, no permission). Closing false is an error, so check the handle first:

Guard against a failed open

<?php

$handle = fopen("data.txt", "r");

if ($handle === false) {
    echo "Could not open the file.";
} else {
    // ... read from the file ...
    fclose($handle);
    echo "Done.";
}

Example 3: Close inside a loop

When you process many files, close each handle as soon as you finish so descriptors don't pile up:

Close each handle inside the loop

<?php

$files = ["a.txt", "b.txt", "c.txt"];

foreach ($files as $name) {
    $handle = fopen($name, "r");
    if ($handle !== false) {
        // ... process the file ...
        fclose($handle);   // closed before the next iteration opens another
    }
}

Example 4: Check the return value

In code where a failed close matters (for example, writing to a network stream), inspect the return value:

Handle a failed close

<?php

$handle = fopen("report.txt", "w");
fwrite($handle, "Generated report\n");

if (fclose($handle) === false) {
    echo "Failed to close the file — data may not have been saved.";
} else {
    echo "File saved successfully.";
}

Common gotchas

  • Don't use a handle after closing it. Once fclose() runs, the resource is invalid; calling fread(), fwrite(), or fclose() on it again fails.
  • One fclose() per fopen(). Closing the same handle twice triggers an error on the second call.
  • fclose() does not delete the file. It only releases the pointer. Use unlink() to remove a file from disk.
  • Auto-close ≠ free for free. Relying on PHP's end-of-script auto-close is fine for tiny scripts, but a loop that opens without closing can hit the OS file-descriptor limit long before the script ends.
  • fopen() — open a file and get the handle fclose() later closes.
  • fwrite() — write data to an open handle.
  • fread() / fgets() — read from an open handle.
  • feof() — test whether you've reached the end of the file.
  • PHP File Handling — the bigger picture of working with files.

Conclusion

fclose() closes an open file pointer, flushing any buffered writes and releasing the underlying system resource. While PHP closes handles automatically when a script finishes, closing them explicitly — especially inside loops and long-running scripts — keeps your file descriptors from running out and guarantees your data reaches disk. Pair every fopen() with an fclose(), and guard against a failed open before you read, write, or close.

Practice

Practice
What is the purpose of the fclose() function in PHP?
What is the purpose of the fclose() function in PHP?
Was this page helpful?