Is it possible to always write in the first line using PHP?

Asked

Viewed 191 times

3

Well, I want to make PHP always write in the first line in a text file, that is, I want it to never write down or then in the text.

Simply put, I want him to write before the texts.

It is possible?

  • It is possible yes, what is [doing, has some specific doubt. You can [Dit] the question and put your code and where you are having trouble.

2 answers

4

Yes, it’s possible, but there’s no specific command, if that’s what you mean.

file_put_contents($filename, $data . file_get_contents($filename));

Don’t forget to put the line break: $data = 'Belezura\n';

  • This method consumes a lot of memory, but in some cases it is faster. This answer offers an alternative if you are dealing with very large files: http://stackoverflow.com/a/3332403/1840019

1

I believe that’s what you wish to do:

$nome_arquivo = 'c:\texto.txt';
$arquivo      = file($nome_arquivo);
$arquivo      = implode("\n",$arquivo);
$string_a_ser_inserida = "PROPRIEDADE DE : PABLO MARTINS" . PHP_EOL;
file_put_contents($nome_arquivo , $string_a_ser_inserida . $arquivo);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.