fclose

(PHP 4, PHP 5, PHP 7)

fclose关闭一个已打开的文件指针

说明

bool fclose ( resource $handle )

handle 指向的文件关闭。

参数

handle

文件指针必须有效,并且是通过 fopen()fsockopen() 成功打开的。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

Example #1 一个简单 fclose() 例子

<?php

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

fclose($handle);

?>

参见

  • fopen() - 打开文件或者 URL
  • fsockopen() - 打开一个网络连接或者一个Unix套接字连接

User Contributed Notes

tom dot vom dot berg at online dot de 29-Apr-2014 05:38
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();
jgotti 08-Sep-2012 03:25
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); ?>
crrodriguez at opensuse dot org 12-May-2010 06:05
Note that since PHP 5.3.2 fclose() no longer unlock open file descriptors at shutdown.
sineld at sineld dot com 23-Nov-2006 01:50
Do not forget, if you are going to read the contents of the file which you have already written via fwrite() or else you have to close the file first.
mark at markvange * com 05-May-2006 06:17
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.
James R. Steel 28-Nov-2005 06:02
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
kit dot lester at lycos dot co dot uk 18-Sep-2005 02:34
fclose() also clears any locks on the file, so if another process was being kept waiting for the lock to be cleared, fclose()ing will allow the other process to continue.

[Another "just-in-case" reason to habitually fclose() all files as soon as practical!]
jricher at jacquesricher dot com 01-Feb-2005 03:06
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.
daniel7 dot martinez at ps dot ge dot com 11-Sep-2001 01:06
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.