我需要登录一堆服务器尾部日志并将输出写入文件(输出需要附加到文件中,以便文件包含来自每个服务器的条目)。以下是我到目前为止的情况:
<?php
chdir('c:/php');
include('Net/SSH2.php');
$servers=array('server1','server2','server3','server4');
foreach ($servers as &$value1) {
$server=$value1.".example.com";
$ssh = new Net_SSH2($server);
if (!$ssh->login('user', 'passwd')) {
echo 'unable to login '.$value1;
continue;
}
$file_name="/apps/logs/was/gws1/perf.log";
$line= $ssh->exec("tail -50 $file_name");
$rawLines = explode("\n", $line);
$lines = array();
$fp = fopen('C:/perf_log.csv', 'w');
echo $rawLines;
fputcsv($fp, $rawLines);
fclose($fp);
}
exit
?>
这是在文件中创建一行。我需要$ rawFiles中的每一行都是perf_log.csv中的新行。我有什么想法可以在php中做到这一点吗?
答案 0 :(得分:0)
您在打开文件时使用w
,这会截断以前存在的所有行:
Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
您真正想要的是a
:
Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.