PHP Doku:: Schreibt den Ausgabepuffer in eine Datei - function.fflush.html

Verlauf / Chronik / History: (7) anzeigen

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

Ein Service von Reinhard Neidl - Webprogrammierung.

Dateisystem-Funktionen

<<feof

fgetc>>

fflush

(PHP 4 >= 4.0.1, PHP 5)

fflushSchreibt den Ausgabepuffer in eine Datei

Beschreibung

bool fflush ( resource $handle )

Diese Funktion erzwingt das Schreiben des gesamten Ausgabepuffers in die von dem Dateizeiger handle angegebene Datei.

Parameter-Liste

handle

Der Zeiger auf eine Datei muss gültig sein und auf eine Datei verweisen, die vorher erfolgreich mit fopen() oder fsockopen() geöffnet (und nicht bereits von fclose() geschlossen) wurde.

Rückgabewerte

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

Beispiele

Beispiel #1 File write example using fflush()

<?php
$filename 
'bar.txt';

$file fopen($filename'r+');
rewind($file);
fwrite($file'Foo');
fflush($file);
ftruncate($fileftell($file));
fclose($file);
?>

Siehe auch


2 BenutzerBeiträge:
- Beiträge aktualisieren...
oryan at zareste dot com
31.10.2005 1:48
Just a note that fflush takes care of the cache already, at least on my server.  Using clearstatcache before reading a previously written file also worked, but lagged the output time much more noticeably (by two or three seconds), probably due to excess cache erasing beyond files.
jzho327 at cse dot unsw dot edu dot au
27.01.2003 9:27
Sorry, the note I previously added regarding fflush() has a bug!

when you read file by calling
$fp = fopen("myfile", "a+");

and you wish to read the content, you have to call
rewind($fp) before you attemp to read it. (Silly me) Or else if you tried something like
$fp = fopen("myfile", "a+");
$content = fread($fp);

// show that content is in fact null
if ($content == NULL)
 print "but it must be first put to rewind<br>\n";

will result

but it must be first put to rewind.
[new line]

in fact, if you are not careful (like me), and you opened it with
$fp = fopen("myfile", "r+");
$content = fread($fp);
..
// do something with content
..
then you tried to again
$content = fread($fp);
without rewind, you'd get NULL again. This sounds trivial, but if you work with several file resources, with freads and fopens everywhere, you can get easily lost. So becareful.



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",...)