As far as I am understanding c/c+ mode, I am expecting behaviour like ‘a/a+ mode, so the file shouldn’t be truncated, and any fwrite() result should be prepended to existing file.
In fact the file seems to be truncated anyway as the file always contains only tle last fwrite() content anyway.
Is it a possible bug in my PHP version (7.0), or I am misunderstanding something?
<?php
$fp = fopen($fpath,'c+');
fwrite($fp, date("H:i:s")." testn");
fclose($fp);
2
Answers
What makes you think that this should behave different? According to the documentation, using
c
the pointer is “positioned on the beginning of the file”. Starting to write from that specific position, you would always override whatever is already present in that fileMaybe a small addition to what has been said:
file
test.txt
is NOT truncated to zero length(asw
orw+
would do onfopen()
),it still contains string(4):
test
Now see what happens when we write one character to the file using
c+
file
test.txt
now contains:best
, the file pointer was positioned at the beginning of the file and only overwrites the first character in the original –test
has becomebest