2

The files inside a folder will be with the utf-8 names like:

  • 日本.txt

While i use this code to list the files inside the dir:

$dir = new DirectoryIterator($path);
foreach ($dir as $filename) {
    echo $filename . "<br>";
}

And i also have correct html header like:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

But anyway, the final output is being like following:

  • ??.txt

How can i make it work?

夏期劇場
  • 17,821
  • 44
  • 135
  • 217
  • 1
    Have you emitted a `Content-Type` header indicating that you'll be serving content with the same character set encoding as the character set encoding of the file names? – Charles Feb 16 '12 at 09:34
  • @Charles yes, please read again the question. I've added it. – 夏期劇場 Feb 16 '12 at 09:39
  • Replace the `echo $filename` with `echo urlencode($filename)` -- does the output match the expected UTF-8 codepoints? For example, "日" seems to be [U+65E5](http://www.fileformat.info/info/unicode/char/65e5/index.htm), which should URL-encode as `%E6%97%A5` – Charles Feb 16 '12 at 09:42
  • 1
    When i use `echo urlencode($filename)`, the output is then, `%3F%3F.txt`. – 夏期劇場 Feb 16 '12 at 09:45
  • `%3F` is a question mark. That's pretty darn astounding. What happens if you use [`opendir()`](http://php.net/opendir) and [`readdir()`](http://php.net/readdir) instead of a `DirectoryIterator`? Also, is the server on *nix or Windows? – Charles Feb 16 '12 at 09:47
  • Same output with `opendir()` and `readdir()`. I'm testing with XAMPP on Windows. – 夏期劇場 Feb 16 '12 at 09:50
  • Okay. [This blog post](http://www.rooftopsolutions.nl/blog/filesystem-encoding-and-php) suggests that PHP + Unicode filenames + Windows = incorrect behavior. The comments are a trainwreck, but they provide some insight into what PHP is doing wrong. I'm not (yet) going to post this as an answer, as I should have been in bed two hours ago and I'm sure that some wizard will drop in overnight with more knowledge for you. – Charles Feb 16 '12 at 09:53
  • 1
    See http://stackoverflow.com/q/2947941/469210 for detailed answer for this issue on Windows. – borrible Feb 16 '12 at 10:14
  • Yeah, this is ultimately a dupe of that question. Blah. – Charles Feb 16 '12 at 17:25

0 Answers0