使用fputcsv()在csv文件顶部插入一行? [英] Use fputcsv() to insert a row at the top of a csv file?

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

问题描述

非常简单的问题.我知道如何使用 fputcsv()将一行插入到csv中,但是它将其插入到底部.有没有办法使用它在csv的顶部插入内容?

Pretty simple question. I know how to use fputcsv() to insert a row into a csv but it inserts it at the bottom. Is there a way to use it to insert something at the top of the csv?

我需要将标题添加到现有的csv文件中.

I need to add a header to an existing csv file.

任何对此的帮助都会很棒.

Any help on this would be great.

谢谢!

推荐答案

fputcsv()将数据插入文件指针,因此您可以

fputcsv() inserts data at the file pointer, so you can rewind() the pointer to the beginning of the file.

rewind($handle);
fputcsv($handle, $fields);

或者,您可以简单地使用开头的指针来打开文件:

Alternately, you can simply open the file with the pointer at the beginning:

$handle = fopen('yourfile.csv', 'r+');
fputcsv($handle, $fields);

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

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