Fflush not working

I want “something” to appear in “test2.txt” right after the call to fwrite (before sleep finishes). If I use fclose it works, but I dont want to use fclose because it will release my flock. I read that fflush would work for this but nothing happens when I use it. Anyone know what the problem is ?

//Works
$file = fopen(“test2.txt”, “r+”);
fwrite($file, “something”);
fclose($file);
sleep(10);

//Doesn’t work
$file = fopen(“test2.txt”, “r+”);
fwrite($file, “something”);
fflush($file);
sleep(10);

Check the $result returned by fflush(…); and also read the comments in the php Online Manual:

https://www.php.net/manual/en/function.fflush.php

@qrazyneo1 when you post code in the forum, you need to format it. To do so you can either select all the code and click the </> button, or type 3 backticks ``` on a separate line both before and after the block of code.

The result is true

Using ftruncate after fflush solved it. Fflush not even necessary. Seems strange that ftruncate does the job of fflush (despite it’s designed for something else) and weird that fflush doesn’t flush at all.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.