Answer the question
In order to leave comments, you need to log in
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
To put in a file besides the data the first line headings - not an output?
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
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 questionAsk a Question
731 491 924 answers to any question