I have a txt file that is generated on the server and contains newlines. If I view the file in my ftp editor, they are here. If I download it through my ftp client and open in it windows with notepad, I also see the newlines.
But when I try to download it through the browser by forcing a download window, and then open the file, all the new lines are suddenly gone.
This is the code I have now. I also tried to set the content type to application/force-download, but that also made no difference.
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="'.$myFile.'"');
header("Content-Transfer-Encoding: binary");
Any idea why the new lines are only missing when I download it through the browser? But are intact when I view the file on the server, or download it from the ftp and the open in notepad?
Unix line endings (\n) are different than Windows line endings (\r\n). Many FTP programs have an option to automatically convert line endings between operating systems; your browser is not doing this (The browser won’t touch text/plain.)
If you want to be double-sure, you could hexdump the file and look at where a newline ought to be.
Try opening the file with wordpad or notepad++, which respect \n as line endings. Notepad is one of the few tools that still requires \r\n line endings.