fclose() Function is one of the PHP operations used in filesystem and stream workflows. Closes an open stream resource. The examples below show what it does and how to handle its result.
What Is fclose() Function?
Closes an open stream resource.
Its syntax is:
fclose($stream)
The parameters are:
$stream— An open stream resource.
Return value: The declared return type is bool. The handle cannot be used after closing; fclose() does not erase the underlying file.
Important: The handle cannot be used after closing; fclose() does not erase the underlying file.
Example 1: Basic Usage
The basic example focuses on the behavior of fclose() Function: closes an open stream resource. Inputs are defined locally or shown as application-controlled paths.
<?php
$stream = fopen("php://temp", "w+");
fwrite($stream, "Hello");
var_dump(fclose($stream));
The displayed value, file content or return status depends on the input and surrounding environment. Results involving local paths, identifiers or generated files are not fixed values.
Example 2: A Different Use Case
This second example illustrates another input or execution condition for fclose() Function. Notice the different checks, arguments, or output handling.
<?php
$stream = tmpfile();
fwrite($stream, "Temporary text");
var_dump(fclose($stream));
This variation matters because the handle cannot be used after closing; fclose() does not erase the underlying file.
Practical Example: Using the Operation in an Application
The following example demonstrates how fclose() Function fits into a more complete file or archive workflow, including inputs and appropriate checks.
<?php
$stream = fopen("php://memory", "w+");
try { fwrite($stream, "report-ready"); } finally { fclose($stream); }
The operation shown is an illustrative script. Adapt the filenames, required permissions, and error handling to your own controlled application environment before running it.
Important Behavior and Edge Cases
- Function behavior: Closes an open stream resource.
- Limitation: The handle cannot be used after closing; fclose() does not erase the underlying file.
Comparison With Related Functions
| Function or Method | Difference |
|---|---|
fclose() Function | Closes an open stream resource. |
fflush() | Flushes buffered writes to a stream but keeps it open; fclose() closes the stream. |
Use fclose() Function for the specific behavior described above. The related operation, fflush(), serves a different purpose; do not substitute it unless that different behavior is intended.
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Treating stream or file operations as guaranteed complete on the first call. | The handle cannot be used after closing; fclose() does not erase the underlying file. Handle false returns, partial I/O, stream position and closing resources. |
| Treating fflush() as an interchangeable replacement for fclose() Function. | Flushes buffered writes to a stream but keeps it open; fclose() closes the stream. |
Key Takeaways
- Closes an open stream resource.
- The handle cannot be used after closing; fclose() does not erase the underlying file.
- Related operation: fflush() — Flushes buffered writes to a stream but keeps it open; fclose() closes the stream.
fclose() Function is useful when its specific behavior matches your application requirement. Keep paths controlled, check failures, and handle environment-dependent operations deliberately.