V
V
Vladimir2018-02-01 12:44:15
PHP
Vladimir, 2018-02-01 12:44:15

How to overwrite information in CSV file via PHP from second line?

Hello. I have a CSV file in the first line - I should always have the field names.
When the page is refreshed, the data in the file is overwritten, but the first line is always left untouched. How can I implement this? fseek - there by bytes. I didn't manage to do it. file_put_contents - it does not overwrite the data, but adds it to the end - it turns out that the data is duplicated.

<?
$fp = fopen('read.csv', 'w');
foreach ($new as $newone) {
$finish = array($newone[1],$newone[2]);
  fputcsv($fp, $finish);
}
fclose($fp)
?>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Pavel Tuzov, 2018-02-01
@PavelTuzov

To put in a file besides the data the first line headings - not an output?

S
Saboteur, 2018-02-01
@saboteur_kiev

VladimirPortev , When rewriting, just write down this first line first, what is the problem with overwriting the file from scratch each time, along with this first line - it is also static

A
Alexander, 2018-02-01
@AK-VoronM

Try like this:

$file = '/path/to/file/file_name';
$string = 'Тут постоянная строка, которая всегда должна быть первой';
file_put_contents($file, $string); //Создаст или перезапишет файл
foreach ($new as $newone) {
    $string = $newone;
    file_put_contents($file, $string, FILE_APPEND | LOCK_EX); //Добавит строку в файл
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question