Add a new line to a CSV file

前端 未结 4 1547
心在旅途
心在旅途 2020-12-24 04:57

If I have a CSV saved on a server, how can I use PHP to write a given line, say 142,fred,elephants to the bottom of it?

4条回答
  •  别那么骄傲
    2020-12-24 05:31

    You can use an object oriented interface class for a file - SplFileObject http://php.net/manual/en/splfileobject.fputcsv.php (PHP 5 >= 5.4.0)

    $file = new SplFileObject('file.csv', 'a');
    $file->fputcsv(array('aaa', 'bbb', 'ccc', 'ffffdd'));
    $file = null;
    

提交回复
热议问题