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
Release: 2023-03-02 18:54:01
Original
3585 people have browsed it

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;;
}
?>
Copy after login

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!

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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template