如何在PHP中迭代非英语文件名

如何在PHP中迭代非英语文件名,php,directoryentry,directory-listing,Php,Directoryentry,Directory Listing,我有一个包含多个文件的目录,其中许多文件的名称不是英文。我在Windows7中使用PHP 我想用PHP列出文件名及其内容 目前我正在使用directoryinterator和file\u get\u contents。这适用于英文文件名,但不适用于非英文(中文)文件名 例如,我的文件名如下“एक और प्रोब्लेम.你好鶨鶖鵨鶣鎹鎣.eml” DirectoryIterator无法使用->getFilename()获取文件名。 file\u get\u contents也无法打开,即使我在

我有一个包含多个文件的目录,其中许多文件的名称不是英文。我在Windows7中使用PHP

我想用PHP列出文件名及其内容

目前我正在使用
directoryinterator
file\u get\u contents
。这适用于英文文件名,但不适用于非英文(中文)文件名

例如,我的文件名如下“एक और प्रोब्लेम.你好鶨鶖鵨鶣鎹鎣.eml”

  • DirectoryIterator
    无法使用
    ->getFilename()获取文件名。
  • file\u get\u contents
    也无法打开,即使我在其参数中硬编码文件名

  • 我该怎么做呢?

    一定要找到我拥有的文件此脚本:

    $content = scandir($directory);
    $list = "<select size = 5 name ='file' id='file'>\n";
    for($i = 0; $i < count ( $content ); $i ++) {
        $list .= "<option>$content[$i] </option>\n";
    }
    $list .= "</select>\n";
    
    $content=scandir($directory);
    $list=“\n”;
    对于($i=0;$i
    这将成功找到文件:鶨鶖鵨鶣鎹鎣 不过我在Linux发行版上试用过

    要阅读它,请使用: 逐行:

    $lines = file('file.txt');
    //loop through our array, show HTML source as HTML source; and line numbers too.
    foreach ($lines as $line_num => $line) {
    print "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";//or try it without the htmlspecialchars
    }
    
    $lines=file('file.txt');
    //循环遍历我们的数组,将HTML源显示为HTML源;还有行号。
    foreach($line作为$line_num=>$line的行){
    打印“Line#{$Line _num}:”.htmlspecialchars($Line)。“
    \n”//或在不使用htmlspecialchars的情况下尝试 }
    这是不可能的。这是PHP的一个限制。PHP使用Windows API的多字节版本;您只能使用代码页可以表示的字符

    目录内容:

    D:\Users\Cataphract\Desktop\teste2>dir Volume in drive D is GRANDEDISCO Volume Serial Number is 945F-DB89 Directory of D:\Users\Cataphract\Desktop\teste2 01-06-2010 17:16 . 01-06-2010 17:16 .. 01-06-2010 17:15 0 coptic small letter shima follows ϭ.txt 01-06-2010 17:18 86 teste.php 2 File(s) 86 bytes 2 Dir(s) 12.178.505.728 bytes free
    <?php
    exec('pause');
    foreach (new DirectoryIterator(".") as $v) {
        echo $v."\n";
    }
    
    D:\Users\Cataphract\Desktop\teste2>dir 驱动器D中的卷是GrandDisco 卷序列号为945F-DB89 D:\Users\Cataphract\Desktop\teste2目录 01-06-2010 17:16 . 01-06-2010 17:16 .. 01-06-2010 17:15科普特语小写字母shima跟在ϭ.txt后面 01-06-2010 17:18 86 teste.php 2个文件86字节 2个目录12.178.505.728字节可用 测试文件内容:

    D:\Users\Cataphract\Desktop\teste2>dir Volume in drive D is GRANDEDISCO Volume Serial Number is 945F-DB89 Directory of D:\Users\Cataphract\Desktop\teste2 01-06-2010 17:16 . 01-06-2010 17:16 .. 01-06-2010 17:15 0 coptic small letter shima follows ϭ.txt 01-06-2010 17:18 86 teste.php 2 File(s) 86 bytes 2 Dir(s) 12.178.505.728 bytes free
    <?php
    exec('pause');
    foreach (new DirectoryIterator(".") as $v) {
        echo $v."\n";
    }
    
    简短回复:

    在Windows下,您不能使用PHP访问任意文件名;您仅限于那些名称可以用当前选定的“代码页”表示的文件名(请参见“区域和语言选项”、“格式”面板和“管理”选项卡面板“非Unicode程序的语言”)

    更长的答复:

    自Win2000以来,Windows使用UTF-16进行文件编码,但PHP作为“非Unicode感知程序”与底层文件系统进行通信。这意味着存在一个当前“代码页表”,该表可从PHP字符串转换为UTF-16字符串,反之亦然。从PHP中,当前代码页可由setlocale()以语言_country.codepage”,例如:

    setlocale(LC_CTYPE,0)=>“english_United States.1252”

    其中1252是当前从控制面板选择的Windows代码页表;从文件系统检索的文件名使用该代码页进行编码;从PHP生成的文件名必须根据该代码页进行编码。UTF-16文件名使用“最佳匹配代码页”,这是实际字符/单词的近似表示形式,因此您不能信任从文件系统检索到的文件名和路径,因为它们可能会被任意损坏

    参考资料:

    什么是“Windows代码页”


    有关此问题的更多详细信息。

    @lvaro G.Vicario他可以,但他不会有正确的名称。NTFS支持正确的UCS-2文件名,您描述的是一种黑客行为。您的解释再好不过了。我今天学到了很多:)@artifact是否可以通过将集合utf-8添加到.htaccess中来工作?或者这是不可能的问题值得标记正如回答的那样,Artefactor努力提供准确的信息。 dp->fileinfo {dwFileAttributes=32 ftCreationTime={...} ftLastAccessTime={...} ...} dwFileAttributes: 32 ftCreationTime: {dwLowDateTime=2784934701 dwHighDateTime=30081445 } ftLastAccessTime: {dwLowDateTime=2784934701 dwHighDateTime=30081445 } ftLastWriteTime: {dwLowDateTime=2784934701 dwHighDateTime=30081445 } nFileSizeHigh: 0 nFileSizeLow: 0 dwReserved0: 3435973836 dwReserved1: 3435973836 cFileName: 0x02f9409c "coptic small letter shima follows ?.txt" cAlternateFileName: 0x02f941a0 "COPTIC~1.TXT" dp->fileinfo.cFileName[34] 63 '?'