2010-06-01 6 views
14

여러 파일이 포함 된 디렉토리가 있는데 그 중 대부분은 영어 이외의 이름을 사용합니다. Windows 7에서 PHP를 사용하고 있습니다.PHP에서 영어 이외의 파일 이름을 반복하는 방법

PHP를 사용하여 파일 이름과 내용을 나열하고 싶습니다.

현재 DirectoryIteratorfile_get_contents을 사용하고 있습니다. 이것은 영어 파일 이름에는 적용되지만 영어가 아닌 (중국어) 파일 이름에는 적용되지 않습니다.

예를 들어, "나는 파일 이름이", "안녕하세요.", "안녕하세요."와 같은 파일 이름이 있습니다.

  1. DirectoryIterator->getFilename()
  2. file_get_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"; 

:

+0

이 질문에 답변을 부여 할 자격이 있습니다. 예술가는 정확한 정보를 제공하기 위해 많은 노력을 기울였습니다. –

+0

예. Thats는 훌륭한 대답입니다. – Sabya

답변

4

이것은 불가능합니다. 그것은 PHP의 한계입니다. PHP는 멀티 바이트 버전의 Windows API를 사용합니다. 코드 페이지가 표현할 수있는 문자로 제한됩니다.

this answer을 참조하십시오.

디렉토리 내용 :

 
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"; 
} 

테스트 파일 결과 :

 
. 
.. 
coptic small letter shima follows ?.txt 
teste.php 

디버거 출력 :

콜 스택 (PHP 5.3.0) :

 
> php5ts_debug.dll!readdir_r(DIR * dp=0x02f94068, dirent * entry=0x00a7e7cc, dirent * * result=0x00a7e7c0) Line 80 C 
    php5ts_debug.dll!php_plain_files_dirstream_read(_php_stream * stream=0x02b94280, char * buf=0x02b9437c, unsigned int count=260, void * * * tsrm_ls=0x028a15c0) Line 820 + 0x17 bytes C 
    php5ts_debug.dll!_php_stream_read(_php_stream * stream=0x02b94280, char * buf=0x02b9437c, unsigned int size=260, void * * * tsrm_ls=0x028a15c0) Line 603 + 0x1c bytes C 
    php5ts_debug.dll!_php_stream_readdir(_php_stream * dirstream=0x02b94280, _php_stream_dirent * ent=0x02b9437c, void * * * tsrm_ls=0x028a15c0) Line 1806 + 0x16 bytes C 
    php5ts_debug.dll!spl_filesystem_dir_read(_spl_filesystem_object * intern=0x02b94340, void * * * tsrm_ls=0x028a15c0) Line 199 + 0x20 bytes C 
    php5ts_debug.dll!spl_filesystem_dir_open(_spl_filesystem_object * intern=0x02b94340, char * path=0x02b957f0, void * * * tsrm_ls=0x028a15c0) Line 238 + 0xd bytes C 
    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, void * * * tsrm_ls=0x028a15c0, long ctor_flags=0) Line 645 + 0x11 bytes C 
    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) Line 658 + 0x1f bytes C 
    php5ts_debug.dll!zend_do_fcall_common_helper_SPEC(_zend_execute_data * execute_data=0x02bc0098, void * * * tsrm_ls=0x028a15c0) Line 313 + 0x78 bytes C 
    php5ts_debug.dll!ZEND_DO_FCALL_BY_NAME_SPEC_HANDLER(_zend_execute_data * execute_data=0x02bc0098, void * * * tsrm_ls=0x028a15c0) Line 423 C 
    php5ts_debug.dll!execute(_zend_op_array * op_array=0x02b93888, void * * * tsrm_ls=0x028a15c0) Line 104 + 0x11 bytes C 
    php5ts_debug.dll!zend_execute_scripts(int type=8, void * * * tsrm_ls=0x028a15c0, _zval_struct * * retval=0x00000000, int file_count=3, ...) Line 1188 + 0x21 bytes C 
    php5ts_debug.dll!php_execute_script(_zend_file_handle * primary_file=0x00a7fad4, void * * * tsrm_ls=0x028a15c0) Line 2196 + 0x1b bytes C 
    php.exe!main(int argc=2, char * * argv=0x028a14c0) Line 1188 + 0x13 bytes C 
    php.exe!__tmainCRTStartup() Line 555 + 0x19 bytes C 
    php.exe!mainCRTStartup() Line 371 C 

정말 물음표입니까?

 
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 '?' 

예! 그것은 캐릭터 # 63입니다.

+0

이름을 읽고 단일 바이트로 쓸 수는 없습니까? –

+0

@ Álvaro G. Vicario 그가 할 수는 있었지만 적절한 이름은 없었습니다. NTFS는 적절한 UCS-2 파일 이름을 지원합니다. 설명하는 것은 해킹입니다. – Artefacto

+0

당신의 설명은 더 좋을 수 없습니다. 오늘 많이 배웠습니다 :) –

0

나는이 스크립트를 가지고있는 파일을 발견 수행 鶨 鶖 鵨 鶣 鎹 鎣 내가 비록 리눅스 배포판 여기에 그것을 시도 ..

에 그것을 사용하여 읽어보십시오 : 라인별로 :

$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 
} 
+0

예, 문제는 Windows입니다. – Artefacto

3

짧은 답글 :

Windows에서는 PHP로 임의의 파일 이름에 액세스 할 수 없습니다. 현재 선택된 "코드 페이지"(지역 및 언어 옵션 ","형식 "패널 및"관리 "탭 패널"비 유니 코드 프로그램 용 언어 "참조)로 표현할 수있는 파일 이름으로 제한됩니다.

긴 답변 :

Windows가 UTF-16 Win2000의 이후 파일 인코딩에 사용하지만, PHP는 "비 유니 코드 인식 프로그램"과 같은 기본 파일 시스템과 통신. 즉, PHP 문자열에서 UTF-16 문자열로 또는 그 반대로 변환되는 현재 "코드 페이지 테이블"이 있음을 의미합니다. PHP에서 현재 코드 페이지는, 예를 들어, "language_country.codepage"형태 setlocale에()에 의해 검색 될 수

은 setlocale (LC_CTYPE, 0) ==> "english_United States.1252"

1,252 현재 제어판에서 선택된 Windows 코드 페이지 테이블입니다. 파일 시스템에서 검색된 파일 이름은 해당 코드 페이지를 사용하여 인 코드됩니다. PHP에서 생성 된 파일 이름은 해당 코드 페이지에 따라 인코딩되어야합니다. UTF-16 파일 이름이 실제 문자/단어의 대략적인 표현 인 "최적의 코드 페이지"를 사용하여 PHP 문자열로 변환되므로 파일 이름과 경로를 신뢰할 수 없다는 사실로 인해 훨씬 ​​더 복잡합니다 임의로 맹 글링 될 수 있으므로 파일 시스템에서 검색됩니다.

참고 :

http://en.wikipedia.org/wiki/Windows_code_page

"Windows 코드 페이지는"무엇.

https://bugs.php.net/bug.php?id=47096 이 문제에 대해 자세히 알아보십시오.

관련 문제