How to add lines to a CSV file without overwriting it
Asked Answered
H

1

5
$file = fopen("contacts.csv","w");
foreach(array_unique($matches[0]) as $email) {
    fputcsv($file,explode(',',$email));
}
fclose($file);

The above code generates a CSV file. How can I update the CSV from the last recorded line without overwriting from the beginning?

Hemocyte answered 21/7, 2015 at 15:2 Comment(0)
P
15

Change "w" to "a" in the fopen. It changes "write" into "append".

"append" opens the file and writes at the end of the file, not from the beginning like "write".

i.e. change this line

$file = fopen("contacts.csv","w");

to

$file = fopen("contacts.csv","a");

Poaceous answered 21/7, 2015 at 15:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.