<?php
$myFile = "/home/user1/www/cgi-bin/mytext.txt";
$lines = file($myFile);
$lines[3] = $_POST['title'];
file_put_contents($myFile , implode("\n", $lines));
?>
I would change only the row "number 3" without changing the other rows, where mistake?
i have input mytext.txt:
after lunch script this is the ouput mytext.txt:
You can either use the FILE_IGNORE_NEW_LINES
constant in file()
or don't use more newlines:
file_put_contents($myFile , implode('', $lines));
Or just this as it will be imploded for you:
file_put_contents($myFile , $lines);
But you'll need to add a newline here:
$lines[3] = $_POST['title'] . "\n";
So this might be the most straight forward:
$lines = file($myFile, FILE_IGNORE_NEW_LINES);
$lines[3] = $_POST['title'];
file_put_contents($myFile , implode("\n", $lines));