There is a file for example list.txt in which there are several lines, for example:

Строка1 Строка2 Строка3 

You need to do this in php:

 $a = строка1; Удалить первую строку; Сохранить новый list.txt 

The task seems to be simple, but something is constantly failing. thanks for the answer

    1 answer 1

    In the simplest case (if the file size is not large) the solution might look like this

     <?php $lines = file('list.txt'); unset($lines[0]); file_put_contents('list.txt', implode('', $lines)); 

    If the file is voluminous and it is not an option to write it completely into memory, you can recreate the temporary file next to it without the first line, and after it has been successfully created, rename it to list.txt

     <?php $fd = fopen('list.txt', 'r'); $tm = fopen($tmpname = tempnam('.', 'list'), 'w+'); if($fd === false) exit('Не могу открыть целевой файл'); if($tm === false) exit('Не могу открыть временный файл'); $i = 0; while (($line = fgets($fd)) !== false) { if(++$i == 1) continue; fwrite($tm, $line); } fclose($fd); fclose($tm); rename($tmpname, 'list.txt'); 
    • Here's another question, if in the file there are 1000-2000 lines of 7-9 characters, how best to do it? - Andrew Baliy
    • 2
      @AndrewBaliy The first version runs completely in RAM, the second on the hard disk. Therefore, the first option is always faster than the second. The size of the memory allocated to the script is determined by the directive memory_limit in php.ini, by default it is 128MB, if your file is obviously smaller than this value, you can safely use the first option. If the file is half a gigabyte, you will have to either use the second option, or if this is a one-time task on your local machine, remove the restriction on the script memory. - cheops