简体   繁体   English

php readdir问题与日语文件名

[英]php readdir problem with japanese language file name

I have the following code 我有以下代码

<?php
if ($handle = opendir('C:/xampp/htdocs/movies')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            echo $file."<br />\n";
        }
    }
    closedir($handle);
}
?>

When it does have mb language such as japanese, it doesn't display properly instead it display like kyuukyoku Choujin R ?????~? 当它确实有像日语这样的mb语言时,它不能正常显示而是显示像kyuukyoku Choujin R ?????~? rather then kyuukyoku Choujin R 究極超人あ~る 而不是kyuukyoku Choujin R研极极人あ〜る

Anyway to make it display the correct name or make it still download-able by others? 无论如何要让它显示正确的名称或使其仍可由其他人下载?

Thanks for helping me :) 谢谢你帮助我:)

I can't speak definitively for PHP, but I suspect it's the same basic problem as with Python 2 had (before later adding special support for Unicode string filenames). 我不能肯定地说PHP,但我怀疑它与Python 2的基本问题相同(之前添加了对Unicode字符串文件名的特殊支持)。

My belief is that PHP is dealing with filenames using the standard C library 'open'-et-al functions, which are byte-based. 我相信PHP正在使用标准C库'open'-et-al函数来处理文件名,这些函数是基于字节的。 On Windows (NT) these try to encode the real Unicode filename using the system codepage. 在Windows(NT)上,这些尝试使用系统代码页对真实的Unicode文件名进行编码。 That might be cp1252 (similar to ISO-8859-1) for Western machines, or cp932 (similar to Shift-JIS) on Japanese machines. 这可能是西方机器的cp1252(类似于ISO-8859-1),或日本机器上的cp932(类似于Shift-JIS)。 For any characters that don't exist in the system codepage you will get a '?' 对于系统代码页中不存在的任何字符,您将获得“?” character, and you'll be unable to refer to that file. 字符,你将无法引用该文件。

To get around this problem PHP would have to do the same as Python 3.0 and start using Unicode strings for filenames (and everything else), using the '_wopen'-et-al functions to get native-Unicode access to the filenames under Windows. 为了解决这个问题,PHP必须像Python 3.0一样开始使用Unicode字符串作为文件名(以及其他所有内容),使用'_wopen'-et-al函数来获取Windows下文件名的本机Unicode访问。 I expect this will happen in PHP6, but for the moment you're probably pretty much stuffed. 我希望这会发生在PHP6中,但目前你可能已经填满了。 You could change the system codepage to cp932 to get access to the filenames, but you'd still get '?' 您可以将系统代码页更改为cp932以访问文件名,但您仍然可以获得'?' characters for any other Unicode characters not in Shift-JIS, and in any case you really don't want to make your application's internal strings all Shift-JIS as it's quite a horrible encoding. 其他任何Unicode字符的字符不是在按住Shift JIS,在任何情况下,你真的不想让你的应用程序的内部串所有的移位,JIS,因为它是一个相当可怕的编码。

If it's your own scripts choosing how to store files, I'd strongly suggest using simple primary-key-based filenames like '4356' locally, putting the real filename in a database, and serving the files up using rewrites/trailing path parts in the URL. 如果你自己的脚本选择如何存储文件,我强烈建议在本地使用简单的基于主键的文件名,如'4356',将真实的文件名放在数据库中,并使用重写/尾随路径部分提供文件。 URL。 Keeping user-supplied filenames in your own local filenames is difficult and a recipe for security disasters even without having to worry about Unicode. 将用户提供的文件名保存在您自己的本地文件名中是困难的,并且即使不必担心Unicode也会导致安全灾难。

As @bobince mentioned, PHP returns filenames in the specified encoding for System Locale , which is used by non-Unicode aware applications. 正如@bobince所提到的,PHP以System Locale的指定编码返回文件名, System Locale由非Unicode感知应用程序使用。 If the character doesn't exist in the current system encoding, the filename will contain '?' 如果当前系统编码中不存在该字符,则文件名将包含“?” instead and will not be accessible. 相反,将无法访问。

You can try installing php-wfio.dll at https://github.com/kenjiuno/php-wfio , and refer to files via the wfio:// protocol. 您可以尝试在https://github.com/kenjiuno/php-wfio上安装php-wfio.dll ,并通过wfio://协议引用文件。

You missed two other references to the $file variable, mate, but that's for the better as I think I may've discovered a slightly more efficient method; 你错过了对$ file变量的另外两个引用,mate,但是这更好,因为我认为我可能已经发现了一种稍微更有效的方法; give this a try: 尝试一下:

<?php
if ($handle = opendir('C:/xampp/htdocs/movies')) {
    while (false !== ($file = readdir($handle))) {
        $file = mb_substr($file, mb_strrpos($file, '/') + 1);
        if ($file != "." && $file != "..") {
            echo $file . "<br />\n";
        }
    }
    closedir($handle);
}
?>

Replace any instance of $file with mb_substr($file, mb_strrpos($file, '/') + 1) and you should be good to go. mb_substr($ file,mb_strrpos($ file,'/')+ 1)替换$ file的任何实例,你应该好好去。 Huzzah for multi-byte encoding! Huzzah用于多字节编码!

I think Windows uses UTF-16 for file names. 我认为Windows使用UTF-16作为文件名。 So try the mb_convert_encoding function to convert from the internal encoding to your output encoding: 因此,请尝试使用mb_convert_encoding函数将内部编码转换为输出编码:

// convert from UTF-16 to UTF-8
echo mb_convert_encoding($file, 'UTF-8', 'UTF-16');

Maybe you have to change some settings first (see mb_get_info ). 也许你必须先改变一些设置(参见mb_get_info )。

sorry :) 抱歉:)

tries this: 尝试这个:

<?php if ($handle = opendir('C:/xampp/htdocs/movies')) { while (false !== ($file = readdir($handle))) { $filename_utf16 = iconv( "iso-8859-1", "utf-16", $file); if ($filename_utf16 != "." && $filename_utf16 != "..") { echo $filename_utf16 . "<br />\\n"; } } closedir($handle); } ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM