Home >Backend Development >PHP Problem >What should I do if the Chinese file name of fopen under PHP is garbled?

What should I do if the Chinese file name of fopen under PHP is garbled?

coldplay.xixi
coldplay.xixiOriginal
2020-07-21 11:48:323463browse

Solution to the garbled Chinese file name in fopen under php: First encode the file name, the code is [$fileName = iconv('UTF-8', 'GBK', $fileName)]; then save the original Chinese file name; finally, create the file through file name encoding.

What should I do if the Chinese file name of fopen under PHP is garbled?

Solution to the garbled Chinese file name in fopen under php:

Encode the file name.

<?php
$fileName = __DIR__ . &#39;\测试.txt&#39;;
$fileName = iconv(&#39;UTF-8&#39;, &#39;GBK&#39;, $fileName);
$fp = fopen($fileName, &#39;w&#39;);
fwrite($fp, &#39;这是中文内容&#39;);
fclose($fp);
echo $fileName.&#39;<br>&#39;;
if(file_exists($fileName)){
    echo &#39;hhhhh&#39;;
}
?>

Result:

What should I do if the Chinese file name of fopen under PHP is garbled?

But look at the file name output on the page:

What should I do if the Chinese file name of fopen under PHP is garbled?

So when you want When outputting a file name on the page and needing to save the file, you can save the original Chinese file name first, and then create the file through file name encoding.

Related learning recommendations: PHP programming from entry to proficiency

The above is the detailed content of What should I do if the Chinese file name of fopen under PHP is garbled?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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