Well, for there to be "lines" at all in the file in the first place, then there must already be \n characters on the end of each of them. When the file is read into an array with the file() function, it does not strip those newline characters. Therefore, using the "\n" character when imploding them back to the file is adding a superfluous newline.
Use this instead:
$file = implode("", $file);