SunshinePHP 2020 CFP Started

fclose

(PHP 4, PHP 5, PHP 7)

fcloseFerme un fichier

Description

fclose ( resource $handle ) : bool

Ferme le fichier représenté par le pointeur handle.

Liste de paramètres

handle

Le pointeur de fichier doit être valide et doit avoir été correctement ouvert par la fonction fopen() ou la fonction fsockopen().

Valeurs de retour

Cette fonction retourne TRUE en cas de succès ou FALSE si une erreur survient.

Exemples

Exemple #1 Exemple avec fclose()

<?php

$handle 
fopen('somefile.txt''r');

fclose($handle);

?>

Voir aussi

  • fopen() - Ouvre un fichier ou une URL
  • fsockopen() - Ouvre un socket de connexion Internet ou Unix

add a note add a note

User Contributed Notes 6 notes

up
14
jricher at jacquesricher dot com
14 years ago
It is a GOOD_THING to check the return value from fclose(), as some operating systems only flush file output on close, and can, therefore, return an error from fclose(). You can catch severe data-eating errors by doing this.

I learned this the hard way.
up
7
jgotti
6 years ago
In case you have some trouble to properly disconnect some client streams opened with stream_socket_server / stream_select you should give a try to stream_socket_shutdown.

<?php stream_socket_shutdown($clientStream,STREAM_SHUT_RDWR); ?>
up
3
daniel7 dot martinez at ps dot ge dot com
17 years ago
Generally, it's always a good idea to close a file when you're done with it. It's very easy for something to go wrong and corrupt a file that hasn't been closed properly. If you're concerned about efficiency, the overhead is negligible.
up
-1
tom dot vom dot berg at online dot de
5 years ago
if you want to daysychain a filehandle through some functions and each function is allowed to close th file you might look in a following function first, if the handle is still valid.

Opening a file, there often will be used a code like

if (!$fh = fopen($filename, $mode)) return false;

But if you possably have closed the file and you want to check that, a smililar statement would not work.

DOES NOT WORK:   if (!$fh)  end_of_chain();

use beter: if (is_resource($fh))   end_of_chain();
up
-5
mark at markvange * com
13 years ago
It is very important to make sure you clear any incoming packets out of the incoming buffer using fread() or some equivalent.  Although you can call fclose() the socket does not actually shut down until the inbound packets have been cleared.  This can lead to some confusion.
up
-5
James R. Steel
13 years ago
In response to kumar mcmillan 'gotcha' note below, we get a different result on a W2K machine:

<?php
$file_pointer
= fopen('textfile.dat', 'r');
fclose($file_pointer);
echo
'$file_pointer is resource = ' . (is_resource($file_pointer) ? 'true': 'false');
?>

output:
$file_pointer is resource = false
To Top