I’m having trouble with the encoding of a file, it seems. It’s a textfile created using vim via SSH on a CentOS Server. When viewing the file in a browser there are problems with the encoding of the file.
I created a testfile, which explains this behavior:
res.tobscore.com/test.txt
And this is how I want the output to be like (this is just an html file using special characters to display the umlaute correctly):
res.tobscore.com/test.html
Using the command file and cat in the Terminal presented the following output:
user>file test.txt
test.txt: UTF-8 Unicode English text
user>cat test.txt
This is a testfile. I'm using the German Umlaute and the euro sign, to test
the encoding.
Euro - €
Scharfes S - ß
Ae - Ä
Oe - Ö
Ue - Ü
As you can see it’s utf-8 unicode and is displayed correctly. Do you have any suggestion, why my browsers(Firefox and Chrome) have trouble displaying it? Using my tablet (set up in German) checking it with the native Browser showed correct results, but trying it with Chrome displayed the same horrible/wrong output.
Is there a way to set the encoding, so displaying it in every environment would present the same output?
Your server will most likely send the .txt file as Content-Type: text/plain, but no character set. Thus, the browser has to pick something (most likely ASCII, iso-8859-1 or iso-8859-15) and will display the UTF-8 bytes as garbage.
One workaround is to wrap your text-file in a little PHP script and send the correct encoding with it:
readfile() will dump the contents of test.txt unaltered to your browser.
Note that is the webserver that picks the Content-Type based on the extension (.txt); you can probably change that, but you’d have to dig deep in the configuration files.