一般在php中會使用fopen進行生成文件,但是當文件名存在中文時保存文件會出現(xiàn)中文亂碼。
源代碼:
<?php
$fileName = __DIR__ . '\測試.txt';
$fp = fopen($fileName, 'w');
fwrite($fp, '這是中文內容');
fclose($fp);
echo $fileName.'<br>';
if(file_exists($fileName)){
echo 'hhhhh';
}
?>
結果:
文件名是亂碼
解決:
對文件名進行編碼。
<?php
$fileName = __DIR__ . '\測試.txt';
$fileName = iconv('UTF-8', 'GBK', $fileName);
$fp = fopen($fileName, 'w');
fwrite($fp, '這是中文內容');
fclose($fp);
echo $fileName.'<br>';
if(file_exists($fileName)){
echo 'hhhhh';
}
?>
結果:
文件名非亂碼
但是看頁面輸出的文件名:
輸出的文件名為亂碼
所以當要在頁面上輸出文件名又需要保存文件時,可以先保存原先的中文文件名,再通過文件名編碼進行創(chuàng)建文件。