If you always know that the file ends with ?> and nothing more, you can:
$userfile = "lib/data/users.php"; $fh = fopen($userfile, 'r+'); $addUser = "string_for_new_user ?>"; fseek($fh, -2, SEEK_END); fwrite($fh, $addUser); fclose($fh);
To further enhance the answer: you're going to want to open your file in mode
r+ because of the following note regarding
If you have opened the file in append (a or a+) mode, any data you write to the file will always be appended, regardless of the file position, and the result of calling fseek() will be undefined.
fseek($fh, -2, SEEK_END) will place the position at the end of the file, and then move it backwards by 2 bytes (the length of