Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

fclose

Closes an open file.

Syntax

fclose(resource $stream): bool

Parameters

ParameterTypeDescription
$streamresourceA 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)
saved

Notes

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

  • fopen — Opens a file for reading or writing piece by piece.
  • fwrite — Writes a string to an open file.
  • fgets — Reads one line from an open file.

In the tutorial

  • Reading and writing files — Working with files in PHP — writing, reading, appending, reading line by line, checking existence and deleting.

Thanks for reading!

Donate to the site — we work hard for you.