PHP popen and proc_open not returning binary output
When using popen or proc_open to run the following command to list the contents of a windows share:
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开发者_StackOverflow SO_RCVBUF=8192 SO_SNDBUF=8192' -d 0 '//server/sharename' -c 'dir "dirpath\*"' 2>/dev/null
If the file in the share has the § character (ASCII 21 Decimal) when it encounters the special character it skips data until the next line.
$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);
Expected Output:
. 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
Real Output:
. 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
The LANG environment variable for the shell was not set to unicode.
putenv('LANG=en_GB.UTF-8');
or
setlocale(LC_CTYPE, "UTF8", "en_GB.UTF-8");
精彩评论