Fwrite working inconsistently

software development


I have a php script that writes a string to a file. It sometimes works when loading the URL in my browser and sometimes it doesn’t. How do I know? I echoed the string to the browser - which shows the correct string AND the returned value of fwrite, or the number of bytes written, but when I check the file either by opening the file in my browser or via ftp, the file is not consistently updated - sometimes it is, but not always.

I created a simpler version of my fwrite script - creating a string, and writing it to a file, and it works by loading the URL in the browser - updating my file - every single time. The only difference is that I echo the fwrite as below.

Works 100% example

<?php $test = time(); $file = fopen("test.txt","w"); $contents = time()." - Hello World. Testing! - ".time(); echo fwrite($file,$contents); echo "contents: ".$contents."
"; fclose($file); ?>

Works sometimes

<?php error_reporting(E_ALL); $outstr = ''; $outstr = $outstr.'.... some long string about 2000 characters long.....'; $nxtStr = ''; $outstr = $outstr.$nxtStr; echo $outstr; $file = fopen("markers-nxt.xml","w"); $size = fwrite($file,$outstr); echo "wrote: ".$size; fclose($file); ?>

Obviously, the files are local. error_reporting(E_ALL) throws no errors. Can someone tell me what’s going on? Is there a flushing problem? if so, what is the best way to flush?


The problem wasn’t the file write, but it was the XML header declaration. For some reason, declaring the header was causing problems.

If found that solution in stackoverflow.com.

Thx. everyone for looking at this.