Home > Backend Development > PHP Tutorial > Regarding the problem of garbled characters when the PHP fopen function creates Chinese-named documents on Windows systems

Regarding the problem of garbled characters when the PHP fopen function creates Chinese-named documents on Windows systems

WBOY
Release: 2016-07-30 13:31:23
Original
1228 people have browsed it

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:

PHP script (UTF-8 encoding)

<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>
Copy after login

Execute the above script and browse the newly created document:
Regarding the problem of garbled characters when the PHP fopen function creates Chinese-named documents on Windows systems
As you can see, the document name is garbled, but the content in the document is not garbled.

Solution

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:
不Regarding the problem of garbled characters when the PHP fopen function creates Chinese-named documents on Windows systems

Finally

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:
Regarding the problem of garbled characters when the PHP fopen function creates Chinese-named documents on Windows systems

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template