doudun2565 2013-01-25 01:49
浏览 145
已采纳

PHP:如何使用fputcsv在csv文件的顶部添加新行?

I want how can I add newly one new row at the top of the csv file becase in my csv file output its populated with data from database and what I want to happen is that I want to add corresponding column names for every type of data.

Here is my code as of now:

    <?php
include 'database.php';
$db = new database();

$data = $db->exportdatabase();

$file = fopen('php://output', 'w');
foreach ($data as $items)
{
    fputcsv($file, $items, ',');
}

header('Content-Description: File Transfer');
header("Content-Type: application/csv") ;
header("Content-Disposition: attachment; filename=data.csv");
header("Expires: 0");
fclose($file);
exit;
?>

Here is my csv data looks like

Image

  • 写回答

1条回答 默认 最新

  • dpxyfa4718 2013-01-25 01:58
    关注

    You can try before foreach set column names

    Or using fseek function

    fseek($file, 0);
    fputcsv($file, $titles, ',');
    

    fseek Seeks on a file pointer

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入