Search code examples
phptext-filesmonospace

File download breaks formatting of txt file


I am having issues with a text file when it is downloaded that it actually breaks the formatting of the file. The script I have to make the text file uses str_pad to make the number of spaces needed between the items that are being parsed into the text file.

http://www.lokalradio.no/rapport/gramo/upload/gramorapport-Radio%20Metro%20Mj%C3%B8sbyene-opprettet%2016.08.2018.txt It appears to show correctly in the browser, but when the file is downloaded using Save Page As and then reopened in a text editor, the formatting is broken.

Is there something I can do in the php script or anything at all to prevent this from happening? The text file is made using this script

$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, str_pad($res['dato'], 19));
fwrite($fh, str_pad($res['spilletid'], 6));
fwrite($fh, str_pad($res['songid'], 77));
fwrite($fh, str_pad($res["tittel"],50));
fwrite($fh, str_pad($res["artist"],50));
fwrite($fh, "\n");
fclose($fh);

When the file is written the charset is set to ISO-8859-1 to have full support for Nordic characters. The download from browser might break the formatting because it assumes UTF-8.


Solution

  • Well. I believe your file is not broken but it is the editor which you are using. Maybe you are using notepad. Try opening with Notepad++. See the image which shows how I see the downloaded file

    enter image description here