PHP Doku:: Schließt einen offenen Dateizeiger - function.fclose.html

Verlauf / Chronik / History: (2) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzDateisystemrelevante ErweiterungenDateisystemDateisystem-Funktionenfclose

Ein Service von Reinhard Neidl - Webprogrammierung.

Dateisystem-Funktionen

<<diskfreespace

feof>>

fclose

(PHP 4, PHP 5)

fcloseSchließt einen offenen Dateizeiger

Beschreibung

bool fclose ( resource $handle )

Die Datei, auf die handle zeigt, wird geschlossen.

Parameter-Liste

handle

Der Datei-Zeiger muss gültig sein, d.h. die Datei, auf die gezeigt wird muss zuvor mit fopen() oder fsockopen() geöffnet worden sein.

Rückgabewerte

Gibt bei Erfolg TRUE zurück. Im Fehlerfall wird FALSE zurückgegeben.

Beispiele

Beispiel #1 Ein einfaches fclose() Beispiel

<?php

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

fclose($handle);

?>

Siehe auch

  • fopen() - Öffnet eine Datei oder URL
  • fsockopen() - Stellt eine Internet- oder Unix-Domain-Socket-Verbindung her


9 BenutzerBeiträge:
- Beiträge aktualisieren...
crrodriguez at opensuse dot org
12.05.2010 6:05
Note that since PHP 5.3.2 fclose() no longer unlock open file descriptors at shutdown.
sineld at sineld dot com
23.11.2006 13: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
5.05.2006 6: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.
williamhamby at williamhamby dot net
8.04.2006 3:19
Trying to understand how 'end foreach' and 'endwhile' differ, I've encountered a problem I can't solve by myself. The following is a fairly easy stock portfolio script. Everything works, except at the end where I want to calculate the average of the gains as represented by $gain[$i].

<?php
$quantity
="3";
$stocks=array("iso","grn","bdgr.pk");
$buydates=array("3 jan 2006","1 feb 2006","3 apr 2006");
$prices=array("0.32","0.20","0.95");
$recommends=array("hold","strong buy","buy");
$i=0;
echo
"<tr>";
echo
"\n<th align='center'>ticker</th>";
echo
"\n<th align='center'>buy date</th>";
echo
"\n<th align='center'>price $</th>";
echo
"\n<th align='center'>recommend</th>";
echo
"\n<th align='center'>value $</th>";
echo
"\n<th align='center'>change %</th>\n</tr>\n";
foreach(
$stocks as $stock) {
$fp=fopen("http://","r");
$data=fgetcsv($fp,1000,",");
$values=$data[1];
echo
"<tr>\n<td align='center'>".$stock."</td>";
echo
"\n<td align='center'>".$buydates[$i]."</td>";
echo
"\n<td align='center'>".$prices[$i]."</td>";
echo
"\n<td align='center'>".$recommends[$i]."</td>";
echo
"\n<td align='center'>".$values."</td>\n<td align='center'>";
echo
$gain[$i]=round(((($values-$prices[$i])/$prices[$i])*100),2);
echo
"</td>\n</tr>";
fclose ($fp);
$i++;
}
echo
"\n<tr>\n<td align='center'>".$gain[$i]."<td>\n</tr>\n";
?>

Help?
James R. Steel
28.11.2005 18: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.09.2005 14: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
1.02.2005 15: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.
kumar mcmillan
30.09.2004 21:10
gotcha:

<?php

$file_pointer
= fopen('data.dat', 'r');
fclose($file_pointer);

echo
'$file_pointer is resource = ' . (is_resource($file_pointer) ? 'true': 'false');

?>

output:
$file_pointer is resource = true
daniel7 dot martinez at ps dot ge dot com
11.09.2001 1: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.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)