Simple PHP function to remove the last line from text file not working

前端 未结 1 470
[愿得一人]
[愿得一人] 2021-01-16 21:04

I have a text file named test with only 2 lines:

1
2

I want to be able to remove the last line from the file so I use the foll

1条回答
  •  北海茫月
    2021-01-16 21:32

    file function only returns you the contents of a file (as an array) - and whatever you do with that array, only changes the array, not the file. To persist the changes, write the contents back to the file:

    $filename = 'test.txt';
    $arr = file($filename);
    if ($arr === false) {
      die('Failed to read ' . $filename);
    }
    array_pop($arr);
    file_put_contents($filename, implode(PHP_EOL, $arr));
    

    0 讨论(0)
提交回复
热议问题