I set my header as follows:
header( 'Content-Type: text/html; charset="utf-8"' );
and then output a local file on my server to the browser using the following code-segment:
$content = file_get_contents($sPath);
$content = mb_convert_encoding($content, 'UTF-8');
echo $content;
The files I have on the server are created by lua and thus, the output of the following is FALSE
(before conversion):
var_dump( mb_detect_encoding($content) );
The files contain some characters like ™
(™
) etc. and these appear as plain square boxes in browsers. I've read the following threads which were suggested as similar questions and none of the variations in my code helped:
- PHP File Get Contents & String Encoding (No gzip in my case, the files are plain
.txt
s) - file_get_contents() Breaks Up UTF-8 Characters (Tried first two top-rated solutions, neither worked. The third one isn't applicable for my case)
- file_get_contents() converts UTF-8 to ISO-8859-1 (No stream is there to provide as context)
There seem to be no problems when I simply use the following:
header( 'Content-Type: text/html; charset="iso-8859-1"' );
// setting path here
$content = file_get_contents($sPath);
echo $content;