4

I am using Windows 2003 to write some PHP code. I use XAMPP Portable (copy to D:). The problem:

$path = 'D:\ebooks';
$all_file = scandir($path);
foreach ($all_file as $file) {
  if (is_dir("$path/$file") && $file != '.' && $file != '..') {
    echo $file . "<br />\n";
  }
}

When I call the script (with browser), I didn't see any directories (within D:\ebooks) containing a Unicode character (I tested with Vietnamese, Japanese, Chinese, Czech).

But if I remove is_dir("$path/$file"), the directories display with many strange characters and many ??? characters.

How can I solve the problem?

cmbuckley
  • 40,217
  • 9
  • 77
  • 91
Quang Rom
  • 43
  • 4

2 Answers2

6

Unfortunately, there are a lot of bugs related to PHP access to a windows filesystem. While Windows does store the filenames as UTF-16, PHP's internals use the much older ANSI api's. So it's best to only do stuff with filenames that are in the ascii range, or switch to a different operating system.

Evert
  • 93,428
  • 18
  • 118
  • 189
  • 1
    @cbuckley: Normally I'd discourage people from saying that, but in this case it's just fact that PHP on windows has really shitty filesystem support. – Evert Apr 04 '12 at 17:12
  • i have this problem in linux php 7.1. please check link: https://bugs.php.net/bug.php?id=74958 – user3770797 Jul 21 '17 at 17:36
0

This might help with the Czech, it won't help with CJK (still looking for that myself). Buggy "Rationale": UTF-8 is ok, as long as it's mappable to the windows character set.

PHP is unpleasantly clear about this:

scandir() and readdir() do not support Unicode filenames

http://bugs.php.net/bug.php?id=34574

«PHP does not support unicode operations until PHP6.»

written in September 2005.

You could also try to grab the dos short name equivalent by using system('dir /X'), parse the results for short and long names and work from there. Display long names, actually accessing (for stat, timestamp and file open of course) with the short names...

Community
  • 1
  • 1
Frank N
  • 9,625
  • 4
  • 80
  • 110