2010-02-05 70 views
1

當使用POPEN或proc_open運行以下命令列出Windows共享的內容:PHP POPEN和proc_open沒有返回二進制輸出

smbclient -N -U 'username%password' -O 'TCP_NODELAY IPTOS_LOWDELAY SO_KEEPALIVE SO_RCVBUF=8192 SO_SNDBUF=8192' -O 'TCP_NODELAY IPTOS_LOWDELAY SO_KEEPALIVE SO_RCVBUF=8192 SO_SNDBUF=8192' -d 0 '//server/sharename' -c 'dir "dirpath\*"' 2>/dev/null 

如果共享文件具有§字符(ASCII 21十進制),當它遇到特殊字符時,它將跳過數據直到下一行。

 $descriptorspec = array(
     0 => array("pipe", "r"), // stdin is a pipe that the child will read from 
     1 => array("pipe", "wb"), // stdout is a pipe that the child will write to 
     2 => array("pipe", "w") // stderr is a file to write to 
    ); 

    $process = proc_open("smbclient -N -U 'username%password' -O 'TCP_NODELAY IPTOS_LOWDELAY SO_KEEPALIVE SO_RCVBUF=8192 SO_SNDBUF=8192' -O 'TCP_NODELAY IPTOS_LOWDELAY SO_KEEPALIVE SO_RCVBUF=8192 SO_SNDBUF=8192' -d 0 '//server/sharename' -c 'dir \"dirpath\*\"' 2>/dev/null", $descriptorspec, $pipes, null, null, array('binary_pipes'=>true)); 
    if(!is_resource($process)) { 
     return false; 
    } 

    echo 'START[['.fread($pipes[1], 10096).']]END'; 
    fclose($pipes[0]); 
    fclose($pipes[1]); 
    fclose($pipes[2]); 
    proc_close ($process); 

預期輸出:

.         D  0 Fri Feb 5 15:31:11 2010 
    ..         D  0 Fri Feb 5 15:31:11 2010 
    Test File § hello.doc    A 233472 Wed Feb 3 15:16:26 2010 
    test.xls         11776 Tue Feb 24 11:14:28 2009 

實際產出:

.         D  0 Fri Feb 5 15:31:11 2010 
    ..         D  0 Fri Feb 5 15:31:11 2010 
    Test File test.xls         11776 Tue Feb 24 11:14:28 2009 

回答

2

shell的LANG環境變量未設置爲Unicode。

putenv('LANG = en_GB.UTF-8');

的setlocale(LC_CTYPE, 「UTF8」, 「en_GB.UTF-8」);