The first mistake is to use the append. You want to write in the archive and not add something to the end of the file.
The other thing is that you need to find where the first line ends and start writing right after. There are several ways to do this. One of them that is simpler is to write the first line again after reading it. I think you tried to do but actually threw the first line contents on top of the file handler, ie you lost the connection to the file.
Actually, there’s another problem. The second line may be larger or smaller than the original content, so it would be better to write again everything that comes after.
$fn = fopen('teste.txt', 'w'); //note que isto destrói o conteúdo do arquivo
$fc = file($fn);
$fc[1] = '-alteração na segunda linha';
file_put_contents($fn, $fc);
fclose($fn);
I put in the Github for future reference.
I kept the names of the variables so as not to confuse you but I don’t know if it’s just them that confused you. Giving meaningful names to variables help a lot to understand the code.
Finally do not try to access the file concurrently since there is no locking control on this algorithm.
You know the size of the first line or it is given perlo line terminator?
– Maniero
The size of the first line is fixed only two letters being them "<?".
– Rose
I realized that in this case will be irrelevant already the others may vary, so it is better to write the whole file again.
– Maniero
Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?
– Maniero