簡體   English   中英

您如何在php的csv文件中寫入linearray的輸出?

[英]how do you write the output of the linearray in a csv file in php?

我需要登錄到一堆服務器並記錄日志,然后將輸出寫入文件(輸出需要附加到文件中,以便該文件包含來自每個服務器的條目)。 這是我到目前為止的內容:

<?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?

您在打開文件時使用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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM