It is necessary to write a line at the beginning of the text file.
I make a record as follows:

$err_out=$path_catalog.$out; $temp = fopen($err_out, "r+"); $text = "Не удалось скопировать файлы:"; fwrite($temp, $text . "\r\n"); fclose($temp); 

The $text string is written to the file, but the characters of the subsequent (already existing at the time of writing) line are overwritten.
What are the ways to solve this problem?

2 answers 2

Solutions to choose from:

  1. do not write anywhere except at the end of the file
  2. overwrite the entire file. First a new line, then everything in the file before

File systems cannot write before a file, only instead. You can record a new content at the end of a small price, consistent insertion (and not replacement) somewhere other than the end of the file - without overwriting the file is impossible.

You can use any DBMS. For example, embedded sqlite . Then she will take over the problems of recording and a bunch of other rakes and you can read in the order you need by simply specifying the usual order by

    Using the @Gino Pane advice, I solved the problem as follows:

     $text = "Не удалось скопировать файлы: \n"; $text .= file_get_contents($err_out) . PHP_EOL; //читаем и "дописываем" в начало текста file_put_contents($err_out, $text ."\n"); 

    where $ err_out is the file to which you need to add the line $ text
    The result looks like this:

     Не удалось скопировать файлы: copy "..\SourceDotNet\WebClient\WebClient.Web\RSB_WebClient.aspx" to "..\Distrib\WebClient" copy "..\SourceDotNet\WebClient\WebClient.Web\Web.config" to "..\Distrib\WebClient" copy "..\SourceDotNet\WebClient\WebClient.Web\Silverlight.js" to "..\Distrib\WebClient" copy "..\SourceDotNet\WebClient\WebClient.Web\FileUpload.ashx" to "..\Distrib\WebClient"