K
K
Konstantin Mokhov2014-11-20 00:09:03
PHP
Konstantin Mokhov, 2014-11-20 00:09:03

How to edit a CSV file?

Good afternoon!
I use the standard function for writing to CSV

$list = array(
        array('email', $dataS)
    );

    $fp = fopen('file.csv', 'w');

    foreach ($list as $fields) {
        fputcsv($fp, $fields);
    }

But as a result, new lines are not added, but the content is replaced. I sit probably the fourth hour.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
neolink, 2014-11-20
@HtmlMak

You have chosen the wrong mode with which you open the file, see the documentation:
php.net/manual/en/function.fopen.php
you need to specify the mode a instead of w :
$fp = fopen('file.csv', 'a');

S
Sergey Lerg, 2014-11-20
@Lerg

If you only need to append to a file, then open it with the a (append) parameter,
fopen('file.csv', 'a')

B
Boris Benkovsky, 2014-11-20
@benbor

php.net/manual/ru/function.fopen.php read about the mode parameter.
If you want to add exactly to the end of the file, then change 'w' to 'a'.
If you need to change lines inside, you will have to move the pointer manually php.net/manual/en/function.fseek.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question