I'm trying to append a string to a file that contains a json string. In order to do so, I must delete the last bracket "]", and append the new string at the end of the file. This is how I tried to do it:
$fh = fopen($target_file, 'r+') or die("can't open file"); // opens file
$stat = fstat($fh); // get data from statt struct
ftruncate($fh, $stat['size']-1); // remove last char
fseek($fh, SEEK_END); // move file pointer to end
fwrite($fh, $append_str); // write new string
fclose($fh); // close
However, the $append_str gets written to the beginning of the file. What should be different for the append operation to work? (p.s.: using wamp server)