如何在php中编写多行输出?

如何在php中编写多行输出?,php,asterisk,Php,Asterisk,我想将命令的输出写入php文件,但打开该文件时,只写入一行输出 $myfile = fopen("status1.txt", "a+"); $cmd="asterisk -rx 'sip show peers'|greo OK"; $test1=system($cmd); fwrite($myfile, $test1); fclose($myfile); 输出为 1004 /1004 (Unspecified)

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

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

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

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  

但在文件中只写入第一行

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

e、 g

exec(字符串$command[,数组和$output[,int和$return\u 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);

可能需要附加的副本。检查前面提到的问题是否重复comment@TrungNguyen他使用了
a+
表示指针位于文件末尾,并且不会截断为零。在
$test1
之前您是否尝试过使用
\r\n
?您是否尝试过使用
a
而不是
a+
?请阅读。它表示
在成功时返回命令输出的最后一行
。因此,
$test1
将只是命令输出的最后一行。我认为这不是问题所在。
  $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);