fclose
Closes an open file.
Syntax
fclose(resource $stream): boolParameters
| Parameter | Type | Description |
|---|---|---|
| $stream | resource | A file opened with `fopen()`. |
Return value
bool`true` on success.
Examples
Open, write, close:
<?php
$file = sys_get_temp_dir() . '/ref-fclose.txt';
$handle = fopen($file, 'w');
fwrite($handle, 'saved');
var_dump(fclose($handle));
echo file_get_contents($file);Output:
bool(true)
savedNotes
PHP closes files itself when the program ends, but in long-running scripts unclosed files pile up. Close a file as soon as you are done with it.
See also
In the tutorial
- Reading and writing files — Working with files in PHP — writing, reading, appending, reading line by line, checking existence and deleting.