fputcsv adds a line ending on the last element

前端 未结 4 1753
攒了一身酷
攒了一身酷 2020-12-22 05:31

I have a basic PHP script that creates a csv file from an array. Here is an example of the code:

$array = [
    [1,2,3],
    [4,5,6]
];

$handle = fopen(\'te         


        
4条回答
  •  佛祖请我去吃肉
    2020-12-22 06:00

    I found this solution on another question: https://stackoverflow.com/a/8354413/1564018

    $stat = fstat($handle);
    ftruncate($handle, $stat['size']-1);
    

    I added these two lines after fputcsv() and they removed the last new line character, removing the blank line at the end of the file.

提交回复
热议问题