user2935236
user2935236

Reputation: 189

Need help in csv export

I've a strange problem. Actually I am using export csv in yii with ECSVExport. And its working fine. When I export csv then its headers looks something like this.

organisation_path, client_key and so on

Now I need to add one row before header (I mean above titles). So I used below lines after export csv but before save file to specified location.

$handle = fopen($path, 'r+');
fputcsv($handle, array('version', '1'));
fclose($handle);

And after adding this in my csv, added a new row like

version, 1

but my header title became like

on_path, client_key and so on

So now my problem is when I've added new row at beginning then why my csv title gets disturb (I mean before it was organisation_path but after adding row it became on_path). Is there any better way to add row at beginning of my csv file? Actually I don't want to run any loop here as I just want to add one row before header title.

Any help is really appreciated. Thanks.

Upvotes: 0

Views: 69

Answers (1)

u_mulder
u_mulder

Reputation: 54841

Opening file in r+ mode means that

Open for reading and writing; place the file pointer at the beginning of the file.

So, when you write your version string, current content is overwritten. And you can see that lengths of version, 1 and organizati are the same.

So, you can write your version before exporting to file.

Or file_get_contents() of your file, prepend this string with your version string and then with file_put_contents() write new data to a file.

Upvotes: 3

Related Questions