PHP popen和proc_open不返回二进制输出

时间:2010-02-05 15:55:17

标签: php binary popen proc-open

使用popen或proc_open运行以下命令列出Windows共享的内容时:

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 Decimal),则会跳过数据直到下一行。

        $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

1 个答案:

答案 0 :(得分:2)

shell的LANG环境变量未设置为unicode。

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

setlocale(LC_CTYPE,“UTF8”,“en_GB.UTF-8”);