1

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.)

Mattish
  • 13
  • 3

2 Answers2

2

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.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • Looked into the above iconv and mb_convert_encoding which was able to solve my issues. Thank you. – Mattish Aug 23 '11 at 17:14
1

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?

Community
  • 1
  • 1
John Himmelman
  • 21,504
  • 22
  • 65
  • 80