windows的cmd也能用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?php #!/usr/local/bin/php -q function read(){ $fp = fopen ( "php://stdin" , "r" ); while (! feof ( $fp )) { $tmp = fgets ( $fp , 255); $input = $input . $tmp ; } fclose( $fp ); return $input ; } $input = read(); echo $input ; |
php处理文件流
cat a.txt |/usr/local/bin/php in.php
echo.php
<?php
ob_implicit_flush(true);
$i = 0;
while(1){
echo "echo {$i}\n";
sleep(1);
if($i> 2)exit();
$i++;
}
read.php
<?php
/*
function read(){
$fp = fopen("php://stdin", "r");
while(!feof($fp)) {
$tmp = fgets($fp, 255);
$input = $input. $tmp;
}
fclose($fp);
return $input;
}
$input = read();
var_dump($input);
*/
while($stdin = fread(STDIN, 65535)){
var_dump($stdin);
}
可以进行实时的非阻塞读取数据输出,而不是阻塞的读取
php echo.php | php read.php