PHP 处理后 UTF8 无法正确显示
我有一个名为“同”的文件夹(与我使用哪个字符或多少个字符无关),用 php 拉出它后显示不正确的字符或乱码文本。
<?php
mb_internal_encoding('utf-8');
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo $file;
}
}
closedir($handle);
}
echo "同";
?>
返回:
Folder1index.php��同
它显示 2 个“字符”,就好像它没有正确编码为 UTF-8 一样。由于有问题的字符的回显,浏览器正在将页面正常处理为 UTF-8。
这是 php/代码问题还是其他原因阻止其正常显示?(IIS 等)
I have either a folder by the name of "同" (doesn't matter which character I use or how many) after pulling it up with php shows the incorrect character or garbled text.
<?php
mb_internal_encoding('utf-8');
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo $file;
}
}
closedir($handle);
}
echo "同";
?>
Returns:
Folder1index.php��同
It displays 2 'characters' as if it isn't encoded to UTF-8 properly. The browser is processing the page fine as UTF-8 due to the echo of the character in question.
Is this a php/code issue or is something else preventing it displaying properly?(IIS etc.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题不在于脚本或网站的 UTF-8 编码,问题最肯定是文件系统中文件名的编码。
您首先需要了解文件系统级别使用哪种编码。如果您了解,可以将编码的文件名重新编码为 UTF-8(例如
iconv
;mb_convert_encoding
)。然后它们将在您的网站上正常显示。你写的是你正在使用 IIS,所以我假设你在 Windows 上运行。请参阅此相关答案和/或相关问题NTFS 中的文件名存储为什么编码? 了解更多信息。
The problem is not the UTF-8 encoding of your scripts or the website, the problem most certainly is the encoding of the filenames in your file-system.
You first need to learn which encoding is used on the file-system level. If you get in the know you can re-encode the encoded file-names into UTF-8 (e.g.
iconv
;mb_convert_encoding
). They will display fine then on your website.You write you're using IIS, so I assume you're running on windows. Please see this related answer and/or the related question What encoding are filenames in NTFS stored as? for more information.
这是关于在 php 中处理 utf-8 文件名的优秀线程 - 如何使用 UTF-8 字符串在 PHP 中使用文件系统函数?
在排序中,您需要对文件名调用 urlencode/urldecode。但是,建议您使用 drupal 的音译模块 - 不同服务器上的不同字符集?
Here is an excellent thread on handling utf-8 file names in php - How do I use filesystem functions in PHP, using UTF-8 strings?
In sort, you need to call urlencode/urldecode on file names. However its recommended that you use drupal's transliterate module - Different charset on different server?