Search code examples
phpencodingutf-8fwrite

PHP Problems with writing string to text file


I have created form where you can write text.

In PHP it will get the value of this text field and tries to write it to file:

$txtFile = $filePath.$counter."_ans.txt";
$f=fopen($txtFile, "wb");
fwrite($f, $word);
fclose($f);

When I write text in my language, for example қазақ

I launch it. It created .txt file but when I open it, it shows me: қазақ

Question: What I have to do? My Language is Kazakh.


Solution

  • You need to encode your file in UTF-8 you have two solution :

     header('Content-Type: text/html; charset=utf-8'); // Server side
    

    Or

     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> // Client side
    

    On the top of your file.