This what I'm trying to do:
$output = '';
$stream = popen("some-long-running-command 2>&1", 'r');
while (!feof($stream)) {
$meta = stream_get_meta_data($stream);
if ($meta['unread_bytes'] > 0) {
$line = fgets($stream);
$output .= $line;
}
echo ".";
}
$code = pclose($stream);
Looks like this code is not correct, since it gets stuck at the call to stream_get_meta_data()
. What is the right way to check whether the stream has some data to read? The whole point here is to avoid locking at fgets()
.
fgets()
locks because it waits for a "new-line" character. Usestream_get_contents()
with the length argument instead:$line = stream_get_contents($stream, $meta['unread_bytes']);
– Necrotomy