使用php脚本解析输出2>& 1

时间:2011-06-25 17:25:26

标签: php linux

我有一个exec命令,我用来使ffmpeg工作。

$process = exec("/usr/local/bin/ffmpeg -i /home/g/Desktop/cave.wmv -deinterlace 
-acodec libfaac -ab 96k -ar 44100 -vcodec libx264 -s 480x320 -f flv /home/g/Desktop
/file.flv 2>&1 | php testing123.php") ;

在testing123.php中,我使用

$h = fopen('php://stdin', 'r'); $str = fgets($h); fclose($h);
//topic removed

 $sql = 'INSERT INTO table
(id,status) VALUES(?,?)';
$stmt3 = $conn->prepare($sql);
$result=$stmt3->execute(array(rand(),$str));

但是,正如您可能已经猜到我得到一个数据库条目,只有当文件最初从另一个文件中的exec命令执行时。 有没有办法继续执行文件或其他内容,以便每隔几秒就可以将输入到文件的输出插入到我的数据库中?

1 个答案:

答案 0 :(得分:3)

您需要使用while循环。 fgets文档中有一些示例。

$h = fopen('php://stdin', 'r');

while ( ($str = fgets($h)) !== false )
{
  $sql = 'INSERT INTO ...';

  mysql_query($sql);
}

fclose($h);

文档中的一条评论建议使用stream_get_line代替fgets。如果您有兴趣,请点击以下链接:http://www.php.net/manual/en/function.fgets.php#86319

相关问题