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
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));