如何在php中写多行输出?

时间:2015-09-18 09:31:53

标签: php asterisk

我想将命令的输出写入php文件。但是当我打开文件时,只写了一行输出。

  $myfile = fopen("status1.txt", "a+"); 
  $cmd="asterisk -rx 'sip show peers'|greo OK";

  $test1=system($cmd);
  fwrite($myfile, $test1);
  fclose($myfile);

OUTPUT

1004  /1004     (Unspecified)                            D                 0         OK                                  
1005  /1005     (Unspecified)                            D   N             0         OK                                  
1006  /1006     (Unspecified)                            D   N             0         OK                                  
2501  /2501     (Unspecified)                            D                 0         OK                                  
2502  /2502     (Unspecified)                            D   a             0         OK                                  
2503  /2503     (Unspecified)                            D   a             0         OK                                  
2504  /2504     (Unspecified)                            D                 0         OK  

但是在文件中只写了第一行

1 个答案:

答案 0 :(得分:0)

您可以尝试使用exec函数而不是system

e.g。

exec($cmd,scanme);
$scanme = implode("\n",$scanme);

exec(string $ command [,array& $ output [,int& $ return_var]])

你的案子:

  $myfile = fopen("status1.txt", "a+"); 
  $cmd="asterisk -rx 'sip show peers'|greo OK";

  exec($cmd,$test1);
  $test1 = implode("\n",$test1);

  fwrite($myfile, $test1);
  fclose($myfile);