As we all know, PHP's fopen function can create a new document, but if the document you create is named in Chinese and you are using a Windows system, you will find that the Chinese part of the document name will be garbled. Let’s do an experiment:
<code><span><?php </span><span>$fileName</span> = <span>__DIR__</span> . <span>'/中文文档.txt'</span>; <span>//$fileName = iconv('UTF-8', 'GBK', $fileName);</span><span>$fp</span> = fopen(<span>$fileName</span>, <span>'w'</span>); fwrite(<span>$fp</span>, <span>'这是中文内容'</span>); fclose(<span>$fp</span>); <span>?></span></span></code>
Execute the above script and browse the newly created document:
As you can see, the document name is garbled, but the content in the document is not garbled.
Remove the comment in the second sentence of the above PHP script, convert $fileName
from UTF-8 encoding to GBK encoding, and then execute the script again, and find that the document name will not be garbled:
Currently, this situation is only found on Windows systems and is normal on Mac OS/Linux. Therefore, on Unix/Linux systems, there is no need to convert the encoding of the file name. If you insist on converting the file name into GBK encoding, which will cause garbled characters, for example:
Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above introduces the problem of garbled characters when using the PHP fopen function to create Chinese-named documents on Windows systems, including relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.