Search code examples
phpincludefilenames

php include chinese filename


<?php
require_once 'test中文.php';

C:\Wnmp\php\php.exe C:\Wnmp\html\linhe\test0.php

PHP Warning: require_once(test中文.php): failed to open stream: No such file or directory in C:\Wnmp\html\linhe\test0.php on line 4

PHP Stack trace:

PHP   1. {main}() C:\Wnmp\html\linhe\test0.php:0

PHP Fatal error: require_once(): Failed opening required 'test中文.php' (include_path='.;C:\Wnmp\php\pear') in C:\Wnmp\html\linhe\test0.php on line 4

PHP Stack trace:

PHP   1. {main}() C:\Wnmp\html\linhe\test0.php:0

Process finished with exit code 255

the things I know: test中文.php is a blank file;
There's no relation with the pear.
Including english filename is ok!

Thanks!


Solution

  • Handling non-ASCII filenames is very tricky in PHP, as it entirely delegates to the underlying filesystem. How exactly that filename is stored in the filesystem itself depends on your system and may differ widely between different systems. The short answer is: you need to match the encoding of the underlying filesystem. If your PHP code is in a UTF-8 encoded file and therefore the string "test中文.php" is UTF-8 encoded, but the filesystem actually stores the filename encoded in, say, GB18030, then the filenames don't match. You'd have to do something like this:

    $file = 'test中文.php';
    $file = iconv('UTF-8', 'GB18030', $file);
    require_once $file;
    

    But again, since the exact encoding to use may differ between two systems and/or the encoding may be more complicated than a simple iconv call, it's a huge pain in the neck to deal with this. The long answer is much more complicated and usually boils down to: just stick to ASCII filenames.