oussama kamal
oussama kamal

Reputation: 1037

PHP, Inserting a line into CSV using fputcsv

I have a file that has the following structure :

enter image description here

I'm trying to insert a new line at the end using this function :

ini_set('auto_detect_line_endings',true);//At the beginning of the php file
$handle = fopen("File.csv", "r+");
    $line = array(
        [0] => Trussville
        [1] => L01
        [2] => 8765
        [3] => this is , a sample (xx)
    );
    fputcsv($handle, $line, ',', '"'); 
    fclose($handle);

The result I get is as following : enter image description here

Can help me please ? Thanks

Upvotes: 0

Views: 552

Answers (1)

Blablaenzo
Blablaenzo

Reputation: 1057

before writing, position the file pointer to the end of the file, like so:

...
fseek($handle, 0, SEEK_END);
fputcsv($handle, $line, ',', '"');

Upvotes: 1

Related Questions