2016-07-02 2 views
1

FTP에서 다운로드 할 스크립트를 작성 중입니다. 양식에서 파일 및 폴더를 표시해야합니다 .. ftp_nlist를 사용하면 모두 함께 모이기는하지만 누구인지 알고 싶습니다. 찾을 수 없습니다. 쉬운 방법은이 작업을 수행합니다 :ftp_nlist ... 파일인지 폴더인지 어떻게 알 수 있습니까?

코스 is_file 먼 파일을 작동하지 않습니다 is_dir의
$contents = ftp_nlist($connection, $rep); 
$dossiers =array(); 
$fichiers = array(); 
foreach($contents as $content){ 
    //if folder 
    if (is_folder($content)) $dossiers[] = $content; 
    //si file 
    if(is_filex($content)) $fichiers[] = $content; 
} 

... 내가 좋아하는 ftp_rawlist 뭔가 및 각 결과의 크기 .. 을 발견했습니다

이 :

if($result['size']== 0){ //is dir } 

하지만 빈 파일의 경우 ???

그래서 이드는 폴더 란 무엇이고 파일은 무엇인지 아는 방법은 무엇입니까 ?? 감사합니다.

답변

0

저도 같은 문제를 했어 그리고 이것은 내 솔루션이었다

$conn = ftp_connect('my_ftp_host'); 
ftp_login($conn, 'my_user', 'my_password'); 

$path  = '/'; 

// Get lists 
$nlist = ftp_nlist($conn, $path); 
$rawlist = ftp_rawlist($conn, $path); 

$ftp_dirs = array(); 

for ($i = 0; $i < count($nlist) - 1; $i++) 
{ 
    if($rawlist[$i][0] == 'd') 
    { 
     $ftp_dirs[] = $nlist[$i]; 
    } 
} 

내가 위의 코드를 최적화 할 수 있습니다 알고 대신 두의 한 FTP 요청을하지만 내 목적을 위해이 일을했다. 깨끗한 솔루션을 찾고있는 사람들을위한

, 나는에 ftp_rawlist을 구문 분석하는 스크립트를 발견했습니다이 LINK :

기능

function parse_ftp_rawlist($List, $Win = FALSE) 
{ 
    $Output = array(); 
    $i = 0; 
    if ($Win) { 
    foreach ($List as $Current) { 
     ereg('([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|) +(.+)', $Current, $Split); 
     if (is_array($Split)) { 
     if ($Split[3] < 70) { 
      $Split[3] += 2000; 
     } 
     else { 
      $Split[3] += 1900; 
     } 
     $Output[$i]['isdir']  = ($Split[7] == ''); 
     $Output[$i]['size']  = $Split[7]; 
     $Output[$i]['month']  = $Split[1]; 
     $Output[$i]['day']  = $Split[2]; 
     $Output[$i]['time/year'] = $Split[3]; 
     $Output[$i]['name']  = $Split[8]; 
     $i++; 
     } 
    } 
    return !empty($Output) ? $Output : false; 
    } 
    else { 
    foreach ($List as $Current) { 
     $Split = preg_split('[ ]', $Current, 9, PREG_SPLIT_NO_EMPTY); 
     if ($Split[0] != 'total') { 
     $Output[$i]['isdir']  = ($Split[0] {0} === 'd'); 
     $Output[$i]['perms']  = $Split[0]; 
     $Output[$i]['number'] = $Split[1]; 
     $Output[$i]['owner']  = $Split[2]; 
     $Output[$i]['group']  = $Split[3]; 
     $Output[$i]['size']  = $Split[4]; 
     $Output[$i]['month']  = $Split[5]; 
     $Output[$i]['day']  = $Split[6]; 
     $Output[$i]['time/year'] = $Split[7]; 
     $Output[$i]['name']  = $Split[8]; 
     $i++; 
     } 
    } 
    return !empty($Output) ? $Output : FALSE; 
    } 
} 

사용

// connect to ftp server 
$res_ftp_stream = ftp_connect('my_server_ip'); 

// login with username/password 
$login_result = ftp_login($res_ftp_stream, 'my_user_name', 'my_password'); 

// get the file list for curent directory 
$buff = ftp_rawlist($res_ftp_stream, '/'); 

// parse ftp_rawlist output 
$result = parse_ftp_rawlist($buff, false); 

// dump result 
var_dump($result); 

// close ftp connection 
ftp_close($res_ftp_stream); 
관련 문제