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

Sab*_*bya 15 php directory-listing directoryentry

我有一个目录,其中包含几个文件,其中许多文件都有非英文名称.我在Windows 7中使用PHP.

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

目前我正在使用DirectoryIteratorfile_get_contents.这适用于英文文件名,但不适用于非英文(中文)文件名.

例如,我有像"एकऔरप्रोब्लेम.eml","hello鶨鹙鵨鶣鎹蓥.eml"这样的文件名.

  1. DirectoryIterator 无法使用获取文件名 ->getFilename()
  2. file_get_contents 即使我在其参数中硬编码文件名也无法打开.

我该怎么做?

Art*_*cto 4

这不可能。这是 PHP 的限制。PHP 使用 Windows API 的多字节版本;您仅限于代码页可以表示的字符。

\n\n

看到这个答案

\n\n

目录内容:

\n\n
\nD:\\Users\\Cataphract\\Desktop\\teste2>dir\n 驱动器 D 中的卷是 GRANDEDISCO\n 卷序列号是 945F-DB89\n\n D:\\Users\\Cataphract\\ 的目录Desktop\\teste2\n\n01-06-2010 17:16 .\n01-06-2010 17:16 ..\n01-06-2010 17:15 0 科普特小写字母 shima 跟随 \xcf\xad.txt\n01 -06-2010 17:18 86 teste.php\n 2 个文件 86 字节\n 2 个目录 12.178.505.728 字节可用\n
\n\n

测试文件内容:

\n\n
<?php\nexec(\'pause\');\nforeach (new DirectoryIterator(".") as $v) {\n    echo $v."\\n";\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

测试文件结果:

\n\n
\n.\n..\ncoptic 小写字母 shima 后面是 ?.txt\nteste.php\n
\n\n

调试器输出:

\n\n

调用堆栈(PHP 5.3.0):

\n\n
\n> php5ts_debug.dll!readdir_r(DIR * dp=0x02f94068,dirent *entry=0x00a7e7cc,dirent * * 结果=0x00a7e7c0)第80行C\n php5ts_debug.dll!php_plain_files_dirstream_read(_php_stream *stream=0x02b94280,char * buf= 0x02b9437c , unsigned int count=260, void * * * tsrm_ls=0x028a15c0) 第 820 行 + 0x17 字节 C\n php5ts_debug.dll!_php_stream_read(_php_stream * stream=0x02b94280, char * buf=0x02b9437c, unsigned int size=260, void * * * tsrm_ls=0x028a15c0) 第 603 行 + 0x1c 字节 C\n php5ts_debug.dll!_php_stream_readdir(_php_stream * dirstream=0x02b94280, _php_stream_dirent * ent=0x02b9437c, void * * * tsrm_ls=0x028a15c0) 第 1806 行+ 0x16 字节 C\n php5ts_debug.dll !spl_filesystem_dir_read(_spl_filesystem_object * intern=0x02b94340, void * * * tsrm_ls=0x028a15c0) 第 199 行 + 0x20 字节 C\n php5ts_debug.dll!spl_filesystem_dir_open(_spl_filesystem_object * intern=0x02b94340, char * path=0x0 2b957f0,无效 * * * tsrm_ls=0x028a15c0 ) 第 238 行 + 0xd 字节 C\n php5ts_debug.dll!spl_filesystem_object_construct(int ht=1, _zval_struct * return_value=0x02b91f88, _zval_struct * * return_value_ptr=0x00000000, _zval_struct * this_ptr=0x02b92028, int return_value_used=0 ,无效 * * * tsrm_ls= 0x028a15c0, long ctor_flags=0) 第 645 行 + 0x11 字节 C\n php5ts_debug.dll!zim_spl_DirectoryIterator___construct(int ht=1, _zval_struct * return_value=0x02b91f88, _zval_struct * * return_value_ptr=0x00000000, _zval_struct * this _ptr=0x02b92028,int return_value_used=0, void * * * tsrm_ls=0x028a15c0) 第 658 行 + 0x1f 字节 C\n php5ts_debug.dll!zend_do_fcall_common_helper_SPEC(_zend_execute_data *execute_data=0x02bc0098, void * * * tsrm_ls=0x028a15c0) 第 313 行 + 0x78 字节 C\ n php5ts_debug.dll!ZEND_DO_FCALL_BY_NAME_SPEC_HANDLER( _zend_execute_data * execute_data=0x02bc0098,void * * * tsrm_ls=0x028a15c0) 第 423 行 C\n php5ts_debug.dll!execute(_zend_op_array * op_array=0x02b93888,void * * * tsrm_ls=0x028a15c0) 第 104 行 + 0x11字节 C\n php5ts_debug.dll !zend_execute_scripts(int type=8, void * * * tsrm_ls=0x028a15c0, _zval_struct * * retval=0x00000000, int file_count=3, ...) 第 1188 行 + 0x21 字节 C\n php5ts_debug.dll!php_execute_script(_zend_file_handle * Primary_file= 0x00a7fad4, void * * * tsrm_ls=0x028a15c0) 第 2196 行 + 0x1b 字节 C\n php.exe!main(int argc=2, char * * argv=0x028a14c0) 第 1188 行 + 0x13 字节 C\n php.exe!__tmainCRTStartup( ) 第 555 行 + 0x19 字节 C\n php.exe!mainCRTStartup() 第 371 行 C\n
\n\n

真的是一个问号吗?

\n\n
\ndp->fileinfo\n{dwFileAttributes=32 ftCreationTime={...} ftLastAccessTime={...} ...}\n dwFileAttributes: 32\n ftCreationTime: {dwLowDateTime=2784934701 dwHighDateTime=30081445 }\n ftLastAccessTime: {dwLowDateTime=2784934701 dwHighDateTime=30081445 }\n ftLastWriteTime: {dwLowDateTime=2784934701 dwHighDateTime=30081445 }\n nFileSizeHigh: 0\n nFileSizeLow: 0\n dwReserved0: 34359738 36\n dwReserved1: 3435973836\n cFileName: 0x02f9409c "科普特小写字母 shima遵循 ?.txt"\n cAlternateFileName: 0x02f941a0 "COPTIC~1.TXT"\ndp->fileinfo.cFileName[34]\n63 \'?\'\n
\n\n

是的!这是角色#63。

\n