fclose
(PHP 4, PHP 5)
fclose — Closes an open file pointer
Description
bool fclose
( resource
$handle
)
The file pointed to by handle
is closed.
Parameters
-
handle
-
The file pointer must be valid, and must point to a file successfully opened by fopen() or fsockopen().
Return Values
Returns TRUE
on success or FALSE
on failure.
Examples
Example #1 A simple fclose() example
<?php
$handle = fopen('somefile.txt', 'r');
fclose($handle);
?>
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с файловой системой
- Функции для работы с файловой системой
- basename
- chgrp
- chmod
- chown
- clearstatcache
- copy
- delete
- dirname
- disk_free_space
- disk_total_space
- diskfreespace
- fclose
- feof
- fflush
- fgetc
- fgetcsv
- fgets
- fgetss
- file_exists
- file_get_contents
- file_put_contents
- file
- fileatime
- filectime
- filegroup
- fileinode
- filemtime
- fileowner
- fileperms
- filesize
- filetype
- flock
- fnmatch
- fopen
- fpassthru
- fputcsv
- fputs
- fread
- fscanf
- fseek
- fstat
- ftell
- ftruncate
- fwrite
- glob
- is_dir
- is_executable
- is_file
- is_link
- is_readable
- is_uploaded_file
- is_writable
- is_writeable
- lchgrp
- lchown
- link
- linkinfo
- lstat
- mkdir
- move_uploaded_file
- parse_ini_file
- parse_ini_string
- pathinfo
- pclose
- popen
- readfile
- readlink
- realpath_cache_get
- realpath_cache_size
- realpath
- rename
- rewind
- rmdir
- set_file_buffer
- stat
- symlink
- tempnam
- tmpfile
- touch
- umask
- unlink
Коментарии
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.
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.
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.
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); ?>
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();
Note that from PHP 8.0 onwards, attempting to close a stream that is already closed will throw a fatal TypeError.
Prior to PHP 8, this just caused a warning (that you can silence with @).