【发布时间】:2009-09-17 06:27:42
【问题描述】:
我正在尝试使用 PHP 将简单 Perl 脚本的输出重定向到 Web 浏览器。 Perl 脚本有时需要 2-3 小时才能执行并将输出显示到屏幕上。 到这个时候,我猜 Apache Server 只是超时并显示上述错误消息。
这里是示例代码片段。
# The tmpOutput.txt contains output of a Perl script.
# Read a file using an handle.
$handle = popen("tail -f tmpOutput.txt", 'r');
# Check if read handle has been allocated properly.
if ($handle) {
# to prevent the code from hanging up when the response is slow.
stream_set_blocking($handle, FALSE);
# Set the stream timeout period to 24 hours i.e. 86400 seconds.
stream_set_timeout($handle, 86400);
# Get the available stream meta data information.
$info = stream_get_meta_data($handle);
# While there's no end of file, read the file contents and redirect them to standard display.
while((!feof($handle)) && (!$info['timed_out'])) {
$buffer = fgets($handle);
echo "$buffer<br/>\n";
ob_flush();
flush();
$info = stream_get_meta_data($handle);
}
# Check if some issue while streaming data.
if ($info['timed_out']) {
echo 'Connection timed out!';
}
}
# Close the file handle.
pclose($handle);
【问题讨论】: