PHP:如何使用fputcsv在csv文件顶部新添加一行? [英] PHP: How can I add newly one row at the top of the csv file using fputcsv?

查看:440
本文介绍了PHP:如何使用fputcsv在csv文件顶部新添加一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想如何在csv文件输出中的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;
?>

这是我的csv数据看起来像

Here is my csv data looks like

图像

推荐答案

您可以在foreach设置的列名称之前尝试

You can try before foreach set column names

或使用fseek函数

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

fseek 在文件指针上查找

这篇关于PHP:如何使用fputcsv在csv文件顶部新添加一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆