note 53538 deleted from function.trim by hholzgra
| From: | hholzgra@php.net | Date: | Thu, 02 Aug 2012 12:22:41 +0000 |
| Subject: | note 53538 deleted from function.trim by hholzgra | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-189638@lists.php.net to get a copy of this message | ||
Note Submitter: okumurya at hotmail dot com
----
fread/fwrite blocks program when no data available.
so, you consider use select system call.
following is example.
<?php
/**
*
* write/read pipe
*
* @param resource $w_fp write file handle
* @param resource $r_fp read file handle
* @param string $input
* @return string
*/
function _writeread_pipe(&$w_fp, &$r_fp, $input) {
$output = '';
$write_bytes = 0;
//
while (True) {
if (!isset($r_fp) && !isset($w_fp)) break;
$read = isset($r_fp) ? array($r_fp) : Null;
$write = isset($w_fp) ? array($w_fp) : Null;
// select pipes
$r = stream_select($read,
$write,
$except = Null,
30
);
if ($r === False) {
return PEAR::raiseError('process timeout');
}
// read pipe
if (isset($read) && isset($read[0])) {
do {
$buf = fread($r_fp, 1024);
if (strlen($buf) == 0) {
fclose($r_fp);
$r_fp = Null;
break;
}
$output .= $buf;
} while (True);
}
// write pipe
if (isset($write) && isset($write[0])) {
$r = fwrite($w_fp, substr($input, $write_bytes));
if ($r === False) {
return PEAR::raiseError('process write error');
}
$write_bytes += $r;
if ($write_bytes == strlen($input)) {
fclose($w_fp);
$w_fp = Null;
}
}
}
return $output;
}
?>