Home > php教程 > php手册 > PHP命令行如何判断有管道数据输入

PHP命令行如何判断有管道数据输入

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:08:25
Original
1009 people have browsed it

PHP的STDIN是阻塞操作,直接读取内容的话会造成阻塞.如下代码会一直运行直到有数据输入: $data = stream_get_contents(STDIN); 按理可以通过声明stream_set_blocking(STDIN, FALSE)来操作: stream_set_blocking(STDIN, FALSE);$data = stream_get_contents(ST

PHP的STDIN是阻塞操作,直接读取内容的话会造成阻塞.如下代码会一直运行直到有数据输入:

$data = stream_get_contents(STDIN);
Copy after login

按理可以通过声明stream_set_blocking(STDIN, FALSE)来操作:

stream_set_blocking(STDIN, FALSE);
$data = stream_get_contents(STDIN);
Copy after login

运行依旧不行,其实这是一个Bug,描述:https://bugs.php.net/bug.php?id=34972

通过测试发现,可用通过ftell函数获取STDIN文件句柄指针读/写的位置来判断.

if(ftell(STDIN)===0){
    $data = stream_get_contents(STDIN);
}
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template